diff --git a/src/main/index.ts b/src/main/index.ts index 45d7297a..ac7f17ea 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -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 diff --git a/src/renderer/store/Authorize.ts b/src/renderer/store/Authorize.ts index 14fb09f0..8256e485 100644 --- a/src/renderer/store/Authorize.ts +++ b/src/renderer/store/Authorize.ts @@ -9,27 +9,17 @@ export type AuthorizeState = {} const state = (): AuthorizeState => ({}) const actions: ActionTree = { - 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 => { + 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 } }