Refactoring account setup listenr

This commit is contained in:
kPherox 2019-04-27 19:36:00 +09:00
parent f06327c1e4
commit f1c7b00706
No known key found for this signature in database
GPG Key ID: C04751C2BFA2F62D

View File

@ -17,29 +17,40 @@ interface AccountDoc {
export default class Auth { export default class Auth {
public static ready() { public static ready() {
let clientId: string ipcMain.on("new-account-setup", (event: Event, instance: string) => this.setup(event, instance))
let clientSecret: string }
ipcMain.on("new-account-setup", async (event: Event, instance: string) => {
const SCOPES: string = "read write follow" private static async setup(event: Event, instance: string) {
let url: string | null let appData: OAuth.AppData
try { try {
let appData: OAuth.AppData = await Mastodon.registerApp( const SCOPES: string = "read write follow"
appData = await Mastodon.registerApp(
"TheDesk", "TheDesk",
{ {
scopes: SCOPES scopes: SCOPES
}, },
"https://" + instance "https://" + instance
) )
clientId = appData.clientId } catch (err) {
clientSecret = appData.clientSecret let error: Error = err
url = appData.url event.sender.send(`error`, {
if (url === undefined || url === null) { id: "ERROR_CONNECTION",
message: "Connection error",
meta: error
})
return
}
let url: string | null = appData.url
if (url === null) {
event.sender.send(`error`, { event.sender.send(`error`, {
id: "ERROR_GET_AUTHURL", id: "ERROR_GET_AUTHURL",
message: "Failed to get auth URL to login." message: "Failed to get auth URL to login."
}) })
return return
} }
shell.openExternal( shell.openExternal(
url, url,
{ {
@ -49,6 +60,16 @@ export default class Auth {
if (err) console.log(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) { } catch (err) {
let error: Error = err let error: Error = err
event.sender.send(`error`, { event.sender.send(`error`, {
@ -56,13 +77,9 @@ export default class Auth {
message: "Connection error", message: "Connection error",
meta: error meta: error
}) })
return
} }
})
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) { if (tokenData.accessToken === undefined) {
event.sender.send(`error`, { event.sender.send(`error`, {
id: "ERROR_GET_TOKEN", id: "ERROR_GET_TOKEN",
@ -100,15 +117,5 @@ export default class Auth {
event.sender.send(`login-complete`, newDocs) event.sender.send(`login-complete`, newDocs)
} }
}) })
} catch (err) {
let error: Error = err
event.sender.send(`error`, {
id: "ERROR_CONNECTION",
message: "Connection error",
meta: error
})
}
}
)
} }
} }