feat: handle OAuth token expiration error

This commit is contained in:
EnixCoda 2019-11-17 16:50:33 +08:00
parent 8b086ce132
commit 4a08a0ec92
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD
3 changed files with 18 additions and 4 deletions

View file

@ -138,7 +138,7 @@ function SettingsBarContent() {
</h4>
{!hasAccessToken && (
<a
href="#"
className={'link-button'}
onClick={() => {
// use js here to make sure redirect_uri is latest url
const url = `https://github.com/login/oauth/authorize?client_id=${

View file

@ -173,11 +173,22 @@ async function trySetUpAccessTokenWithCode() {
client_id: oauth.clientId,
client_secret: oauth.clientSecret,
})
const { access_token: accessToken, scope } = res
if (scope !== 'repo' || !accessToken) {
const { access_token: accessToken, scope, error_description: errorDescription } = res
if (errorDescription) {
const TOKEN_EXPIRED_DESCRIPTION = `The code passed is incorrect or expired.`
if (errorDescription === TOKEN_EXPIRED_DESCRIPTION) {
alert(`Gitako: The OAuth token has expired, please try again.`)
} else {
throw new Error(errorDescription)
}
} else if (scope !== 'repo' || !accessToken) {
throw new Error(`Cannot resolve token response: '${JSON.stringify(res)}'`)
}
window.history.pushState({}, 'removed code', window.location.pathname.replace(/#.*$/, ''))
window.history.pushState(
{},
'removed search param',
window.location.pathname.replace(window.location.search, ''),
)
return accessToken
}
} catch (err) {

View file

@ -435,6 +435,9 @@
}
.access-token {
border-bottom: none; // prevent overwrite by github style
.link-button {
cursor: pointer;
}
.hint {
color: #6a737d;
}