/* * Vencord, a Discord client mod * Copyright (c) 2024 Vendicated and contributors * SPDX-License-Identifier: GPL-3.0-or-later */ import * as DataStore from "@api/DataStore"; import { showNotification } from "@api/Notifications"; import { openModal } from "@utils/modal"; import { OAuth2AuthorizeModal, Toasts, UserStore } from "@webpack/common"; import { logger } from "../components/LikesComponent"; export async function authorizeUser(triggerModal: boolean = true) { const isAuthorized = await getAuthorization(); if (isAuthorized === false) { if (!triggerModal) return false; openModal((props: any) => { if (!location) return logger.error("No redirect location returned"); try { const response = await fetch(location, { headers: { Accept: "application/json" } }); const { token } = await response.json(); if (token) { logger.debug("Authorized via OAuth2, got token"); await DataStore.set("ThemeLibrary_uniqueToken", token); showNotification({ title: "ThemeLibrary", body: "Successfully authorized with ThemeLibrary!" }); } else { logger.debug("Tried to authorize via OAuth2, but no token returned"); showNotification({ title: "ThemeLibrary", body: "Failed to authorize, check console" }); } } catch (e: any) { logger.error("Failed to authorize", e); showNotification({ title: "ThemeLibrary", body: "Failed to authorize, check console" }); } } } />); } else { return isAuthorized; } } export async function deauthorizeUser() { const uniqueToken = await DataStore.get>("ThemeLibrary_uniqueToken"); if (!uniqueToken) return Toasts.show({ message: "No uniqueToken present, try authorizing first!", id: Toasts.genId(), type: Toasts.Type.FAILURE, options: { duration: 2e3, position: Toasts.Position.BOTTOM } }); const res = await fetch("https://themes-delta.vercel.app/api/user/revoke", { method: "DELETE", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ token: uniqueToken, userId: UserStore.getCurrentUser().id }) }); if (res.ok) { await DataStore.del("ThemeLibrary_uniqueToken"); showNotification({ title: "ThemeLibrary", body: "Successfully deauthorized from ThemeLibrary!" }); } else { // try to delete anyway try { await DataStore.del("ThemeLibrary_uniqueToken"); } catch (e) { logger.error("Failed to delete token", e); showNotification({ title: "ThemeLibrary", body: "Failed to deauthorize, check console" }); } } } export async function getAuthorization() { const uniqueToken = await DataStore.get>("ThemeLibrary_uniqueToken"); if (!uniqueToken) { return false; } else { // check if valid const res = await fetch("https://themes-delta.vercel.app/api/user/findUserByToken", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ token: uniqueToken }) }); if (res.status === 400 || res.status === 500) { return false; } else { logger.debug("User is already authorized, skipping"); return uniqueToken; } } } export async function isAuthorized(triggerModal: boolean = true) { const isAuthorized = await getAuthorization(); const token = await DataStore.get("ThemeLibrary_uniqueToken"); if (isAuthorized === false || !token) { await authorizeUser(triggerModal); } else { return true; } }