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,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<Account> = await client.get<Account>("/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<Account> = await client.get<Account>("/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)
}
})
}
}