mirror of
https://github.com/h3poteto/whalebird-desktop
synced 2025-02-09 08:18:44 +01:00
refs #174 Refactor auth.js to use async/await
This commit is contained in:
parent
23623f6b99
commit
018cc91db1
@ -1,5 +1,4 @@
|
||||
import Mastodon from 'mastodon-api'
|
||||
import log from 'electron-log'
|
||||
|
||||
const appName = 'whalebird'
|
||||
const scope = 'read write follow'
|
||||
@ -21,63 +20,38 @@ export default class Authentication {
|
||||
this.clientSecret = ''
|
||||
}
|
||||
|
||||
getAuthorizationUrl (domain = 'mastodon.social') {
|
||||
async getAuthorizationUrl (domain = 'mastodon.social') {
|
||||
this.setOtherInstance(domain)
|
||||
return new Promise((resolve, reject) => {
|
||||
Mastodon.createOAuthApp(this.baseURL + '/api/v1/apps', appName, scope)
|
||||
.catch(err => log.error(err))
|
||||
.then((res) => {
|
||||
this.clientId = res.client_id
|
||||
this.clientSecret = res.client_secret
|
||||
const res = await Mastodon.createOAuthApp(this.baseURL + '/api/v1/apps', appName, scope)
|
||||
this.clientId = res.client_id
|
||||
this.clientSecret = res.client_secret
|
||||
|
||||
// TODO: Save order number
|
||||
const json = {
|
||||
baseURL: this.baseURL,
|
||||
domain: this.domain,
|
||||
clientId: this.clientId,
|
||||
clientSecret: this.clientSecret,
|
||||
accessToken: '',
|
||||
username: '',
|
||||
accountId: ''
|
||||
}
|
||||
this.db.insertAccount(json)
|
||||
.then((doc) => {
|
||||
Mastodon.getAuthorizationUrl(this.clientId, this.clientSecret, this.baseURL)
|
||||
.then((url) => {
|
||||
resolve(url)
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
})
|
||||
// TODO: Save order number
|
||||
const json = {
|
||||
baseURL: this.baseURL,
|
||||
domain: this.domain,
|
||||
clientId: this.clientId,
|
||||
clientSecret: this.clientSecret,
|
||||
accessToken: '',
|
||||
username: '',
|
||||
accountId: ''
|
||||
}
|
||||
await this.db.insertAccount(json)
|
||||
const url = await Mastodon.getAuthorizationUrl(this.clientId, this.clientSecret, this.baseURL)
|
||||
return url
|
||||
}
|
||||
|
||||
getAccessToken (code) {
|
||||
return new Promise((resolve, reject) => {
|
||||
Mastodon.getAccessToken(this.clientId, this.clientSecret, code, this.baseURL)
|
||||
.catch(err => reject(err))
|
||||
.then((token) => {
|
||||
const search = {
|
||||
baseURL: this.baseURL,
|
||||
domain: this.domain,
|
||||
clientId: this.clientId,
|
||||
clientSecret: this.clientSecret
|
||||
}
|
||||
this.db.searchAccount(search)
|
||||
.then((rec) => {
|
||||
this.db.updateAccount(rec._id, { accessToken: token })
|
||||
.then((doc) => {
|
||||
resolve(token)
|
||||
})
|
||||
.catch(err => reject(err))
|
||||
})
|
||||
})
|
||||
})
|
||||
async getAccessToken (code) {
|
||||
const token = await Mastodon.getAccessToken(this.clientId, this.clientSecret, code, this.baseURL)
|
||||
const search = {
|
||||
baseURL: this.baseURL,
|
||||
domain: this.domain,
|
||||
clientId: this.clientId,
|
||||
clientSecret: this.clientSecret
|
||||
}
|
||||
const rec = await this.db.searchAccount(search)
|
||||
await this.db.updateAccount(rec._id, { accessToken: token })
|
||||
return token
|
||||
}
|
||||
// TODO: Refresh access token when expired
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user