1
0
mirror of https://github.com/h3poteto/whalebird-desktop synced 2025-02-07 15:28:42 +01:00

refs #859 Add update access token method using refresh token

This commit is contained in:
AkiraFukushima 2019-05-22 22:12:19 +09:00
parent 9dd8ce0bbf
commit b547e09b93

View File

@ -1,4 +1,4 @@
import Mastodon from 'megalodon'
import Mastodon, { OAuth } from 'megalodon'
import Account from './account'
import LocalAccount from '~/src/types/localAccount'
@ -33,7 +33,8 @@ export default class Authentication {
async getAuthorizationUrl(domain = 'mastodon.social'): Promise<string> {
this.setOtherInstance(domain)
const res = await Mastodon.registerApp(
appName, {
appName,
{
scopes: scope,
website: appURL
},
@ -42,9 +43,10 @@ export default class Authentication {
this.clientId = res.clientId
this.clientSecret = res.clientSecret
const order = await this.db.lastAccount()
const order = await this.db
.lastAccount()
.then(account => account.order + 1)
.catch((err) => {
.catch(err => {
console.log(err)
return 1
})
@ -68,7 +70,7 @@ export default class Authentication {
}
async getAccessToken(code: string): Promise<string> {
const tokenData = await Mastodon.fetchAccessToken(this.clientId, this.clientSecret, code, this.baseURL)
const tokenData: OAuth.TokenData = await Mastodon.fetchAccessToken(this.clientId, this.clientSecret, code, this.baseURL)
const search = {
baseURL: this.baseURL,
domain: this.domain,
@ -88,7 +90,20 @@ export default class Authentication {
})
return accessToken
}
// TODO: Refresh access token when expired
async updateAccessToken(id: string): Promise<string> {
const account = await this.db.getAccount(id)
if (!account.refreshToken) {
throw new RefreshTokenDoesNotExist()
}
const data: OAuth.TokenData = await Mastodon.refreshToken(account.clientId, account.clientSecret, account.refreshToken, account.baseURL)
await this.db.updateAccount(id, {
accessToken: data.access_token,
refreshToken: data.refresh_token
})
return data.access_token
}
}
class AuthenticationURLError extends Error {}
class RefreshTokenDoesNotExist extends Error {}