refactor: Use invoke for ipc to get access token
This commit is contained in:
parent
cc1160ea26
commit
abb3aa741c
|
@ -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 => {
|
||||
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 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)
|
||||
if (err) return reject(err)
|
||||
if (isEmpty(doc)) return reject(err)
|
||||
resolve(doc._id)
|
||||
}
|
||||
)
|
||||
})
|
||||
.catch(err => {
|
||||
log.error(err)
|
||||
event.sender.send('error-get-access-token', err)
|
||||
})
|
||||
})
|
||||
|
||||
// environments
|
||||
|
|
|
@ -9,8 +9,7 @@ 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) => {
|
||||
submit: async (_, request: { code: string | null; sns: 'mastodon' | 'pleroma' | 'misskey' }): Promise<string> => {
|
||||
let req = {
|
||||
sns: request.sns
|
||||
}
|
||||
|
@ -19,17 +18,8 @@ const actions: ActionTree<AuthorizeState, RootState> = {
|
|||
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)
|
||||
win.ipcRenderer.removeAllListeners('response-get-access-token')
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
const id = await win.ipcRenderer.invoke('get-access-token', req)
|
||||
return id
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue