thedesk/src/main/Auth.ts

46 lines
1.3 KiB
TypeScript
Raw Normal View History

2019-04-25 00:52:40 +09:00
import { ipcMain, Event, shell } from "electron"
import Mastodon from "megalodon"
export default class Auth {
public static ready() {
2019-04-25 01:04:26 +09:00
let clientId: string
let clientSecret: string
2019-04-25 00:52:40 +09:00
ipcMain.on("new-account-setup", async (event: Event, instance: string) => {
const SCOPES: string = "read write follow"
let url: string | null
Mastodon.registerApp(
"TheDesk",
{
scopes: SCOPES
},
2019-04-25 01:04:26 +09:00
"https://" + instance
2019-04-25 00:52:40 +09:00
).then(appData => {
clientId = appData.clientId
clientSecret = appData.clientSecret
url = appData.url
2019-04-25 01:04:26 +09:00
if (url) {
shell.openExternal(
url,
{
activate: false
},
err => {
if (err) console.log(err)
}
)
} else {
console.log(appData)
2019-04-25 00:52:40 +09:00
}
})
2019-04-25 14:06:36 +09:00
.catch((err: Error) => console.error(err))
2019-04-25 00:52:40 +09:00
})
2019-04-25 01:04:26 +09:00
ipcMain.on("new-account-auth", async (event: Event, code: string, instance: string) => {
2019-04-25 14:06:36 +09:00
Mastodon.fetchAccessToken(clientId, clientSecret, code, "https://" + instance)
2019-04-25 01:04:26 +09:00
.then((tokenData: Partial<{ accessToken: string }>) => {
console.log(tokenData.accessToken)
})
.catch((err: Error) => console.error(err))
})
2019-04-25 00:52:40 +09:00
}
}