From 018cc91db16a56babe493eb25a214a428cf94dd9 Mon Sep 17 00:00:00 2001
From: AkiraFukushima
Date: Sun, 1 Apr 2018 21:58:57 +0900
Subject: [PATCH] refs #174 Refactor auth.js to use async/await
---
src/main/auth.js | 82 +++++++++++++++++-------------------------------
1 file changed, 28 insertions(+), 54 deletions(-)
diff --git a/src/main/auth.js b/src/main/auth.js
index 8c0bf9ef..46d2ce5e 100644
--- a/src/main/auth.js
+++ b/src/main/auth.js
@@ -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
}