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

View File

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