refactor: Use invoke for ipc to get access token

This commit is contained in:
AkiraFukushima 2020-11-28 22:54:43 +09:00
parent cc1160ea26
commit abb3aa741c
2 changed files with 24 additions and 39 deletions

View File

@ -409,26 +409,21 @@ type TokenRequest = {
sns: 'mastodon' | 'pleroma' | 'misskey' sns: 'mastodon' | 'pleroma' | 'misskey'
} }
ipcMain.on('get-access-token', async (event: IpcMainEvent, request: TokenRequest) => { ipcMain.handle('get-access-token', async (_: IpcMainInvokeEvent, request: TokenRequest) => {
const proxy = await proxyConfiguration.forMastodon() const proxy = await proxyConfiguration.forMastodon()
auth const token = await auth.getAccessToken(request.sns, request.code, proxy)
.getAccessToken(request.sns, request.code, proxy) return new Promise((resolve, reject) => {
.then(token => { accountDB.findOne(
accountDB.findOne( {
{ accessToken: token
accessToken: token },
}, (err, doc: any) => {
(err, doc: any) => { if (err) return reject(err)
if (err) return event.sender.send('error-get-access-token', err) if (isEmpty(doc)) return reject(err)
if (isEmpty(doc)) return event.sender.send('error-get-access-token', 'error document is empty') resolve(doc._id)
event.sender.send('response-get-access-token', doc._id) }
} )
) })
})
.catch(err => {
log.error(err)
event.sender.send('error-get-access-token', err)
})
}) })
// environments // environments

View File

@ -9,27 +9,17 @@ export type AuthorizeState = {}
const state = (): AuthorizeState => ({}) const state = (): AuthorizeState => ({})
const actions: ActionTree<AuthorizeState, RootState> = { const actions: ActionTree<AuthorizeState, RootState> = {
submit: (_, request: { code: string | null; sns: 'mastodon' | 'pleroma' | 'misskey' }) => { submit: async (_, request: { code: string | null; sns: 'mastodon' | 'pleroma' | 'misskey' }): Promise<string> => {
return new Promise((resolve, reject) => { let req = {
let req = { sns: request.sns
sns: request.sns }
} if (request.code) {
if (request.code) { req = Object.assign(req, {
req = Object.assign(req, { code: request.code.trim()
code: request.code.trim()
})
}
win.ipcRenderer.send('get-access-token', req)
win.ipcRenderer.once('response-get-access-token', (_, id: string) => {
win.ipcRenderer.removeAllListeners('error-get-access-token')
resolve(id)
}) })
win.ipcRenderer.once('error-get-access-token', (_, err: Error) => { }
console.error(err) const id = await win.ipcRenderer.invoke('get-access-token', req)
win.ipcRenderer.removeAllListeners('response-get-access-token') return id
reject(err)
})
})
} }
} }