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