diff options
author | Vendicated <vendicated@riseup.net> | 2023-10-06 19:40:53 +0200 |
---|---|---|
committer | Vendicated <vendicated@riseup.net> | 2023-10-06 19:43:24 +0200 |
commit | c0f2c974587d75a38e3e753368ef0e2e2be139fd (patch) | |
tree | c1e75f84785d10f207f1f142da0cec6ab01d2443 /src/plugins/reviewDB/auth.tsx | |
parent | 664dd0a9920aa697359b1bb07b98795ff0f1beaf (diff) | |
download | Vencord-c0f2c974587d75a38e3e753368ef0e2e2be139fd.tar.gz Vencord-c0f2c974587d75a38e3e753368ef0e2e2be139fd.tar.bz2 Vencord-c0f2c974587d75a38e3e753368ef0e2e2be139fd.zip |
ReviewDB: proper multi account support
Diffstat (limited to 'src/plugins/reviewDB/auth.tsx')
-rw-r--r-- | src/plugins/reviewDB/auth.tsx | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/plugins/reviewDB/auth.tsx b/src/plugins/reviewDB/auth.tsx new file mode 100644 index 0000000..1d95e47 --- /dev/null +++ b/src/plugins/reviewDB/auth.tsx @@ -0,0 +1,78 @@ +/* + * Vencord, a Discord client mod + * Copyright (c) 2023 Vendicated and contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +import { DataStore } from "@api/index"; +import { Logger } from "@utils/Logger"; +import { openModal } from "@utils/modal"; +import { findByPropsLazy } from "@webpack"; +import { showToast, Toasts, UserStore } from "@webpack/common"; + +import { ReviewDBAuth } from "./entities"; + +const DATA_STORE_KEY = "rdb-auth"; + +const OAuth = findByPropsLazy("OAuth2AuthorizeModal"); + +export let Auth: ReviewDBAuth = {}; + +export async function initAuth() { + Auth = await getAuth() ?? {}; +} + +export async function getAuth(): Promise<ReviewDBAuth | undefined> { + const auth = await DataStore.get(DATA_STORE_KEY); + return auth?.[UserStore.getCurrentUser()?.id]; +} + +export async function getToken() { + const auth = await getAuth(); + return auth?.token; +} + +export async function updateAuth(newAuth: ReviewDBAuth) { + return DataStore.update(DATA_STORE_KEY, auth => { + auth ??= {}; + Auth = auth[UserStore.getCurrentUser().id] ??= {}; + + if (newAuth.token) Auth.token = newAuth.token; + if (newAuth.user) Auth.user = newAuth.user; + + return auth; + }); +} + +export function authorize(callback?: any) { + openModal(props => + <OAuth.OAuth2AuthorizeModal + {...props} + scopes={["identify"]} + responseType="code" + redirectUri="https://manti.vendicated.dev/api/reviewdb/auth" + permissions={0n} + clientId="915703782174752809" + cancelCompletesFlow={false} + callback={async (response: any) => { + try { + const url = new URL(response.location); + url.searchParams.append("clientMod", "vencord"); + const res = await fetch(url, { + headers: new Headers({ Accept: "application/json" }) + }); + const { token, success } = await res.json(); + if (success) { + updateAuth({ token }); + showToast("Successfully logged in!"); + callback?.(); + } else if (res.status === 1) { + showToast("An Error occurred while logging in.", Toasts.Type.FAILURE); + } + } catch (e) { + new Logger("ReviewDB").error("Failed to authorize", e); + } + }} + /> + ); +} |