Replace promise to await & trycatch
This commit is contained in:
parent
ed8514c8c7
commit
c22484db7c
|
@ -1,5 +1,5 @@
|
|||
import { ipcMain, Event, shell, app } from "electron"
|
||||
import Mastodon, { Response, Account } from "megalodon"
|
||||
import Mastodon, { Response, Account, OAuth } from "megalodon"
|
||||
import { join } from "path"
|
||||
import Datastore from "nedb"
|
||||
|
||||
|
@ -20,18 +20,24 @@ export default class Auth {
|
|||
ipcMain.on("new-account-setup", async (event: Event, instance: string) => {
|
||||
const SCOPES: string = "read write follow"
|
||||
let url: string | null
|
||||
Mastodon.registerApp(
|
||||
try {
|
||||
let appData: OAuth.AppData = await Mastodon.registerApp(
|
||||
"TheDesk",
|
||||
{
|
||||
scopes: SCOPES
|
||||
},
|
||||
"https://" + instance
|
||||
)
|
||||
.then(appData => {
|
||||
clientId = appData.clientId
|
||||
clientSecret = appData.clientSecret
|
||||
url = appData.url
|
||||
if (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,
|
||||
{
|
||||
|
@ -41,41 +47,42 @@ export default class Auth {
|
|||
if (err) console.log(err)
|
||||
}
|
||||
)
|
||||
} else {
|
||||
event.sender.send(`error`, {
|
||||
id: "ERROR_GET_AUTHURL",
|
||||
message: "Failed to get auth URL to login."
|
||||
})
|
||||
}
|
||||
})
|
||||
.catch((err: Error) =>
|
||||
} catch (err) {
|
||||
let error: Error = err
|
||||
event.sender.send(`error`, {
|
||||
id: "ERROR_CONNECTION",
|
||||
message: "Connection error",
|
||||
meta: err
|
||||
meta: error
|
||||
})
|
||||
)
|
||||
}
|
||||
})
|
||||
ipcMain.on(
|
||||
"new-account-auth",
|
||||
async (event: Event, code: string, instance: string) => {
|
||||
let url: string = "https://" + instance
|
||||
Mastodon.fetchAccessToken(clientId, clientSecret, code, url)
|
||||
.then((tokenData: Partial<{ accessToken: string }>) => {
|
||||
if (tokenData.accessToken) {
|
||||
try {
|
||||
let tokenData: Partial<{ accessToken: string }> = await Mastodon.fetchAccessToken(clientId, clientSecret, code, url)
|
||||
if (tokenData.accessToken === undefined) {
|
||||
event.sender.send(`error`, {
|
||||
id: "ERROR_GET_TOKEN",
|
||||
message: "Failed to get access token."
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
const client = new Mastodon(
|
||||
tokenData.accessToken,
|
||||
url + "/api/v1"
|
||||
)
|
||||
|
||||
client
|
||||
.get<Account>("/accounts/verify_credentials")
|
||||
.then((resp: Response<Account>) => {
|
||||
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,
|
||||
|
@ -83,6 +90,7 @@ export default class Auth {
|
|||
avatarStatic: you.avatar_static,
|
||||
accessToken: tokenData.accessToken,
|
||||
}
|
||||
|
||||
db.insert(docs, function (err, newDocs) {
|
||||
if (err) {
|
||||
event.sender.send(`error`, {
|
||||
|
@ -93,21 +101,14 @@ export default class Auth {
|
|||
event.sender.send(`login-complete`, newDocs)
|
||||
}
|
||||
})
|
||||
})
|
||||
} else {
|
||||
event.sender.send(`error`, {
|
||||
id: "ERROR_GET_TOKEN",
|
||||
message: "Failed to get access token."
|
||||
})
|
||||
}
|
||||
})
|
||||
.catch((err: Error) =>
|
||||
} catch (err) {
|
||||
let error: Error = err
|
||||
event.sender.send(`error`, {
|
||||
id: "ERROR_CONNECTION",
|
||||
message: "Connection error",
|
||||
meta: err
|
||||
meta: error
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user