Save access token in local storage

This commit is contained in:
AkiraFukushima 2018-03-08 18:00:33 +09:00
parent a3554ec281
commit 77d50b1923
3 changed files with 44 additions and 12 deletions

18
package-lock.json generated
View File

@ -414,7 +414,6 @@
"version": "2.6.0",
"resolved": "https://registry.npmjs.org/async/-/async-2.6.0.tgz",
"integrity": "sha512-xAfGg1/NTLBBKlHFmnd7PlmUW9KhVQIUuSrYem9xzFUZy13ScvtyGGejaae9iAVRiRq9+Cx7DPFaAAhCpyxyPw==",
"dev": true,
"requires": {
"lodash": "4.17.5"
}
@ -4299,6 +4298,18 @@
"integrity": "sha1-FOb9pcaOnk7L7/nM8DfL18BcWv4=",
"dev": true
},
"electron-json-storage": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/electron-json-storage/-/electron-json-storage-4.0.2.tgz",
"integrity": "sha1-wNZizNAbxlZ9YHjxemhCrHQyjOQ=",
"requires": {
"async": "2.6.0",
"lodash": "4.17.5",
"mkdirp": "0.5.1",
"rimraf": "2.6.2",
"rwlock": "5.0.0"
}
},
"electron-localshortcut": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/electron-localshortcut/-/electron-localshortcut-3.1.0.tgz",
@ -12367,6 +12378,11 @@
"aproba": "1.2.0"
}
},
"rwlock": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/rwlock/-/rwlock-5.0.0.tgz",
"integrity": "sha1-iI1qd6M1HMGiCSBO8u4XIgk4Ns8="
},
"rx-lite": {
"version": "4.0.8",
"resolved": "https://registry.npmjs.org/rx-lite/-/rx-lite-4.0.8.tgz",

View File

@ -57,6 +57,7 @@
},
"dependencies": {
"axios": "^0.16.1",
"electron-json-storage": "^4.0.2",
"element-ui": "^2.2.1",
"mastodon-api": "^1.3.0",
"vue": "^2.3.3",

View File

@ -1,8 +1,8 @@
import Mastodon from 'mastodon-api'
import storage from 'electron-json-storage'
const appName = 'whalebird'
const scope = 'read write follow'
const redirectURI = 'urn:ietf:wg:oauth:2.0:oob'
export default class Authentication {
constructor (baseURL = 'https://mstdn.jp') {
@ -12,24 +12,39 @@ export default class Authentication {
}
getAuthorizationUrl () {
return Mastodon.createOAuthApp(this.baseURL + '/api/v1/apps', appName, scope, redirectURI)
return Mastodon.createOAuthApp(this.baseURL + '/api/v1/apps', appName, scope)
.catch(err => console.error(err))
.then((res) => {
console.log('Please save \'id\', \'client_id\' and \'client_secret\' in your program and use it from now on!')
console.log(res)
this.clientId = res.client_id
this.clientSecret = res.client_secret
return Mastodon.getAuthorizationUrl(this.clientId, this.clientSecret, this.baseURL, scope, redirectURI)
const json = {
clientId: this.clientId,
clientSecret: this.clientSecret
}
storage.set('client', json, (err) => {
if (err) throw err
})
return Mastodon.getAuthorizationUrl(this.clientId, this.clientSecret, this.baseURL)
})
}
getAccessToken (code) {
console.log(code)
console.log(this.clientId)
console.log(this.clientSecret)
console.log(this.baseURL)
return Mastodon.getAccessToken(this.clientId, this.clientSecret, code, this.baseURL)
return new Promise((resolve, reject) => {
Mastodon.getAccessToken(this.clientId, this.clientSecret, code, this.baseURL)
.catch(err => reject(err))
.then((token) => {
const json = {
accessToken: token
}
storage.set('token', json, (err) => {
reject(err)
})
resolve(token)
})
})
}
// TODO: Refresh access token when expired
}