Refactoring account setup listenr
This commit is contained in:
parent
f06327c1e4
commit
f1c7b00706
183
src/main/Auth.ts
183
src/main/Auth.ts
|
@ -17,98 +17,105 @@ 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) => {
|
|
||||||
|
private static async setup(event: Event, instance: string) {
|
||||||
|
let appData: OAuth.AppData
|
||||||
|
|
||||||
|
try {
|
||||||
const SCOPES: string = "read write follow"
|
const SCOPES: string = "read write follow"
|
||||||
let url: string | null
|
appData = await Mastodon.registerApp(
|
||||||
try {
|
"TheDesk",
|
||||||
let appData: OAuth.AppData = await Mastodon.registerApp(
|
{
|
||||||
"TheDesk",
|
scopes: SCOPES
|
||||||
{
|
},
|
||||||
scopes: SCOPES
|
"https://" + instance
|
||||||
},
|
)
|
||||||
"https://" + instance
|
} catch (err) {
|
||||||
)
|
let error: Error = err
|
||||||
clientId = appData.clientId
|
event.sender.send(`error`, {
|
||||||
clientSecret = appData.clientSecret
|
id: "ERROR_CONNECTION",
|
||||||
url = appData.url
|
message: "Connection error",
|
||||||
if (url === undefined || url === null) {
|
meta: error
|
||||||
event.sender.send(`error`, {
|
})
|
||||||
id: "ERROR_GET_AUTHURL",
|
return
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
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")
|
shell.openExternal(
|
||||||
let you = resp.data
|
url,
|
||||||
|
{
|
||||||
let db = new Datastore({
|
activate: false
|
||||||
filename: join(app.getPath("userData"), "account.db"),
|
},
|
||||||
autoload: true
|
err => {
|
||||||
})
|
if (err) console.log(err)
|
||||||
|
|
||||||
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
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user