From f1c7b00706261fe9dbcf6d40fa4351f89f0805c8 Mon Sep 17 00:00:00 2001 From: kPherox Date: Sat, 27 Apr 2019 19:36:00 +0900 Subject: [PATCH] Refactoring account setup listenr --- src/main/Auth.ts | 183 ++++++++++++++++++++++++----------------------- 1 file changed, 95 insertions(+), 88 deletions(-) diff --git a/src/main/Auth.ts b/src/main/Auth.ts index 833b9c39..3ad403df 100644 --- a/src/main/Auth.ts +++ b/src/main/Auth.ts @@ -17,98 +17,105 @@ interface AccountDoc { export default class Auth { public static ready() { - let clientId: string - let clientSecret: string - ipcMain.on("new-account-setup", async (event: Event, instance: string) => { + ipcMain.on("new-account-setup", (event: Event, instance: string) => this.setup(event, instance)) + } + + private static async setup(event: Event, instance: string) { + let appData: OAuth.AppData + + try { const SCOPES: string = "read write follow" - let url: string | null - try { - let appData: OAuth.AppData = await Mastodon.registerApp( - "TheDesk", - { - scopes: SCOPES - }, - "https://" + instance - ) - clientId = appData.clientId - clientSecret = appData.clientSecret - url = appData.url - if (url === undefined || url === null) { - event.sender.send(`error`, { - id: "ERROR_GET_AUTHURL", - message: "Failed to get auth URL to login." - }) - return - } - shell.openExternal( - url, - { - activate: false - }, - err => { - if (err) console.log(err) - } - ) - } catch (err) { - let error: Error = err - event.sender.send(`error`, { - id: "ERROR_CONNECTION", - message: "Connection error", - meta: error - }) - } - }) - ipcMain.on( - "new-account-auth", - async (event: Event, code: string, instance: string) => { - try { - let tokenData: Partial<{ accessToken: string }> = await Mastodon.fetchAccessToken(clientId, clientSecret, code, "https://" + instance) - if (tokenData.accessToken === undefined) { - event.sender.send(`error`, { - id: "ERROR_GET_TOKEN", - message: "Failed to get access token." - }) - return - } + appData = await Mastodon.registerApp( + "TheDesk", + { + scopes: SCOPES + }, + "https://" + instance + ) + } catch (err) { + let error: Error = err + event.sender.send(`error`, { + id: "ERROR_CONNECTION", + message: "Connection error", + meta: error + }) + return + } - const client = Client.createAuthClient('http', instance, tokenData.accessToken) + let url: string | null = appData.url + if (url === null) { + event.sender.send(`error`, { + id: "ERROR_GET_AUTHURL", + message: "Failed to get auth URL to login." + }) + return + } - let resp: Response = await client.get("/accounts/verify_credentials") - let you = resp.data - - let db = new Datastore({ - filename: join(app.getPath("userData"), "account.db"), - autoload: true - }) - - let docs: AccountDoc = { - domain: instance, - acct: you.acct, - avatar: you.avatar, - avatarStatic: you.avatar_static, - accessToken: tokenData.accessToken, - } - - db.insert(docs, function (err, newDocs) { - if (err) { - event.sender.send(`error`, { - id: "ERROR_YOU_TRY_ANOTHER_ACCOUNT", - message: "You cannot login already logined account." - }) - } else { - Client.setAuthClient('http', `@${newDocs.acct}@${newDocs.domain}`, client) - event.sender.send(`login-complete`, newDocs) - } - }) - } catch (err) { - let error: Error = err - event.sender.send(`error`, { - id: "ERROR_CONNECTION", - message: "Connection error", - meta: error - }) - } + shell.openExternal( + url, + { + activate: false + }, + err => { + if (err) console.log(err) } ) + + ipcMain.once("new-account-auth", (event: Event, code: string, instance: string) => { + this.auth(event, code, instance, appData.clientId, appData.clientSecret) + }) + } + + private static async auth(event: Event, code: string, instance: string, clientId: string, clientSecret: string) { + let tokenData: Partial<{ accessToken: string }> + try { + tokenData = await Mastodon.fetchAccessToken(clientId, clientSecret, code, "https://" + instance) + } catch (err) { + let error: Error = err + event.sender.send(`error`, { + id: "ERROR_CONNECTION", + message: "Connection error", + meta: error + }) + return + } + + if (tokenData.accessToken === undefined) { + event.sender.send(`error`, { + id: "ERROR_GET_TOKEN", + message: "Failed to get access token." + }) + return + } + + const client = Client.createAuthClient('http', instance, tokenData.accessToken) + + let resp: Response = await client.get("/accounts/verify_credentials") + let you = resp.data + + let db = new Datastore({ + filename: join(app.getPath("userData"), "account.db"), + autoload: true + }) + + let docs: AccountDoc = { + domain: instance, + acct: you.acct, + avatar: you.avatar, + avatarStatic: you.avatar_static, + accessToken: tokenData.accessToken, + } + + db.insert(docs, function (err, newDocs) { + if (err) { + event.sender.send(`error`, { + id: "ERROR_YOU_TRY_ANOTHER_ACCOUNT", + message: "You cannot login already logined account." + }) + } else { + Client.setAuthClient('http', `@${newDocs.acct}@${newDocs.domain}`, client) + event.sender.send(`login-complete`, newDocs) + } + }) } }