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