Update access token and redirect after login

This commit is contained in:
AkiraFukushima 2018-03-09 00:24:18 +09:00
parent 410211b3a9
commit fa3bc25d77
7 changed files with 49 additions and 32 deletions

View File

@ -5,14 +5,17 @@ const appName = 'whalebird'
const scope = 'read write follow'
export default class Authentication {
constructor (db, baseURL = 'https://mstdn.jp') {
constructor (db) {
this.db = db
this.baseURL = baseURL
this.baseURL = ''
this.clientId = ''
this.clientSecret = ''
}
getAuthorizationUrl () {
getAuthorizationUrl (baseURL = 'https://mastodon.social') {
this.baseURL = baseURL
this.clientId = ''
this.clientSecret = ''
return Mastodon.createOAuthApp(this.baseURL + '/api/v1/apps', appName, scope)
.catch(err => console.error(err))
.then((res) => {
@ -22,7 +25,8 @@ export default class Authentication {
const json = {
baseURL: this.baseURL,
clientId: this.clientId,
clientSecret: this.clientSecret
clientSecret: this.clientSecret,
accessToken: ''
}
this.db.insert(json, (err, _) => {
if (err) throw err
@ -37,34 +41,27 @@ export default class Authentication {
Mastodon.getAccessToken(this.clientId, this.clientSecret, code, this.baseURL)
.catch(err => reject(err))
.then((token) => {
this.db.findOne(
{
baseURL: this.baseURL,
clientId: this.clientId,
clientSecret: this.clientSecret
},
(err, doc) => {
if (err) return reject(err)
const json = Object.assign(doc, {
accessToken: token
})
this.db.update(doc, json, (err, _) => {
if (err) return reject(err)
resolve(token)
})
}
)
const search = {
baseURL: this.baseURL,
clientId: this.clientId,
clientSecret: this.clientSecret
}
this.db.update(search, {$set: { accessToken: token }}, {}, (err, num) => {
if (err) return reject(err)
resolve(token)
})
})
})
}
// TODO: ignore unauthorized records which does not have accessToken.
listInstances () {
return new Promise((resolve, reject) => {
this.db.find({}, (err, doc) => {
if (err) return reject(err)
if (empty(doc)) reject(new EmptyTokenError('empty'))
const instances = doc.map((e, i, array) => {
return e.baseURL
return { baseURL: e.baseURL, id: e._id }
})
resolve(instances)
})

View File

@ -59,8 +59,8 @@ app.on('activate', () => {
let auth = new Authentication(db)
// TODO: error handling
ipcMain.on('get-auth-link', (event, _) => {
auth.getAuthorizationUrl()
ipcMain.on('get-auth-link', (event, domain) => {
auth.getAuthorizationUrl(`https://${domain}`)
.catch(err => console.error(err))
.then((url) => {
console.log(url)
@ -73,7 +73,15 @@ ipcMain.on('get-auth-link', (event, _) => {
ipcMain.on('get-access-token', (event, code) => {
auth.getAccessToken(code)
.catch(err => console.error(err))
.then(token => console.log(token))
.then((token) => {
db.findOne({
accessToken: token
}, (err, doc) => {
if (err) return event.sender.send('error-access-token', err)
if (empty(doc)) return event.sender.send('error-access-token', 'error document is empty')
event.sender.send('access-token-reply', doc._id)
})
})
})
ipcMain.on('load-access-token', (event, _) => {

View File

@ -24,6 +24,9 @@ export default {
methods: {
authorizeSubmit () {
this.$store.dispatch('Authorize/submit', this.authorizeForm.code)
.then((id) => {
this.$router.push({ path: `/${id}` })
})
}
}
}

View File

@ -9,9 +9,9 @@
background-color="#545c64"
text-color="#909399"
active-text-color="#ffffff">
<el-menu-item index="1">
<el-menu-item :index="index.toString()" v-for="(instance, index) in instances" v-bind:key="instance.id">
<i class="el-icon-menu"></i>
<span slot="title">First server</span>
<span slot="title">{{ instance.baseURL }}</span>
</el-menu-item>
</el-menu>
<div class="content">
@ -28,7 +28,7 @@ export default {
data () {
return {
isCollapse: true,
defaultActive: '1'
defaultActive: '0'
}
},
computed: {
@ -38,8 +38,11 @@ export default {
},
created () {
this.$store.dispatch('GlobalHeader/listInstances')
.then((instances) => {
return this.$router.push({ path: `/${instances[0].id}` })
})
.catch(() => {
this.$router.push({ path: '/login' })
return this.$router.push({ path: '/login' })
})
},
methods: {

View File

@ -6,7 +6,10 @@
<script>
export default {
name: 'timeline-space'
name: 'timeline-space',
created () {
console.log(this.$route.params.id)
}
}
</script>

View File

@ -21,8 +21,8 @@ export default new Router({
component: require('@/components/GlobalHeader').default,
children: [
{
path: '',
name: 'timeline-space',
path: ':id',
name: 'timeline-space/',
component: require('@/components/TimelineSpace').default
}
]

View File

@ -12,6 +12,9 @@ const Authorize = {
console.log(arg)
resolve(arg)
})
ipcRenderer.on('error-access-token', (event, err) => {
console.log(err)
})
})
}
}