mirror of
https://github.com/EnixCoda/Gitako.git
synced 2026-03-11 08:54:44 +00:00
fix: catch oauth error
This commit is contained in:
parent
75e914de40
commit
aae82fbd2e
2 changed files with 21 additions and 15 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { raiseError } from 'analytics'
|
||||||
import Icon from 'components/Icon'
|
import Icon from 'components/Icon'
|
||||||
import { oauth } from 'env'
|
import { oauth } from 'env'
|
||||||
import * as React from 'react'
|
import * as React from 'react'
|
||||||
|
|
@ -110,19 +111,23 @@ export default class SettingsBar extends React.PureComponent<Props, State> {
|
||||||
}
|
}
|
||||||
|
|
||||||
private async trySetUpAccessTokenWithCode() {
|
private async trySetUpAccessTokenWithCode() {
|
||||||
const search = parseURLSearch()
|
try {
|
||||||
if ('code' in search) {
|
const search = parseURLSearch()
|
||||||
const res = await JSONRequest('https://github.com/login/oauth/access_token', {
|
if ('code' in search) {
|
||||||
code: search.code,
|
const res = await JSONRequest('https://github.com/login/oauth/access_token', {
|
||||||
client_id: oauth.clientId,
|
code: search.code,
|
||||||
client_secret: oauth.clientSecret,
|
client_id: oauth.clientId,
|
||||||
})
|
client_secret: oauth.clientSecret,
|
||||||
const { access_token: accessToken, scope } = res
|
})
|
||||||
if (scope !== 'repo' || !accessToken) {
|
const { access_token: accessToken, scope } = res
|
||||||
throw new Error(`Cannot resolve token response: '${JSON.stringify(res)}'`)
|
if (scope !== 'repo' || !accessToken) {
|
||||||
|
throw new Error(`Cannot resolve token response: '${JSON.stringify(res)}'`)
|
||||||
|
}
|
||||||
|
window.history.pushState({}, 'removed code', window.location.pathname.replace(/#.*$/, ''))
|
||||||
|
this.setState({ accessToken }, () => this.saveToken(''))
|
||||||
}
|
}
|
||||||
window.history.pushState({}, 'removed code', window.location.pathname.replace(/#.*$/, ''))
|
} catch (err) {
|
||||||
this.setState({ accessToken }, () => this.saveToken(''))
|
raiseError(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -113,9 +113,8 @@ export function parseURLSearch(search: string = window.location.search) {
|
||||||
return parsed
|
return parsed
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function JSONRequest(url: string, data: any, method = 'post') {
|
export async function JSONRequest(url: string, data: any, extra: RequestInit = { method: 'post' }) {
|
||||||
return (await fetch(url, {
|
return (await fetch(url, {
|
||||||
method,
|
|
||||||
mode: 'cors',
|
mode: 'cors',
|
||||||
cache: 'no-cache',
|
cache: 'no-cache',
|
||||||
credentials: 'same-origin',
|
credentials: 'same-origin',
|
||||||
|
|
@ -124,8 +123,10 @@ export async function JSONRequest(url: string, data: any, method = 'post') {
|
||||||
Accept: 'application/json',
|
Accept: 'application/json',
|
||||||
},
|
},
|
||||||
redirect: 'follow',
|
redirect: 'follow',
|
||||||
referrer: 'no-referrer',
|
referrerPolicy: 'no-referrer',
|
||||||
|
method: extra.method || 'post',
|
||||||
body: JSON.stringify(data),
|
body: JSON.stringify(data),
|
||||||
|
...extra,
|
||||||
})).json()
|
})).json()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue