aboutsummaryrefslogtreecommitdiff
path: root/src/lib/CommandPalette/authActions.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/CommandPalette/authActions.ts')
-rw-r--r--src/lib/CommandPalette/authActions.ts48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/lib/CommandPalette/authActions.ts b/src/lib/CommandPalette/authActions.ts
new file mode 100644
index 00000000..c306eb34
--- /dev/null
+++ b/src/lib/CommandPalette/authActions.ts
@@ -0,0 +1,48 @@
+import { env } from "$env/dynamic/public";
+import root from "$lib/Utility/root";
+import localforage from "localforage";
+import type { CommandPaletteAction } from "./actions";
+
+export const authActions = (
+ user: string | undefined,
+): CommandPaletteAction[] => {
+ if (user)
+ return [
+ {
+ name: "Log Out",
+ url: "#",
+ preventDefault: true,
+ tags: ["auth", "sign", "out", "user"],
+ onClick: async () => {
+ await localforage.removeItem("identity");
+ await localforage.removeItem("commit");
+
+ document.cookie =
+ "user=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
+
+ window.location.href = root("/api/authentication/log-out");
+ },
+ },
+ ];
+
+ const loginUrl = `https://anilist.co/api/v2/oauth/authorize?client_id=${env.PUBLIC_ANILIST_CLIENT_ID}&redirect_uri=${env.PUBLIC_ANILIST_REDIRECT_URI}&response_type=code`;
+
+ return [
+ {
+ name: "Log In",
+ url: loginUrl,
+ preventDefault: true,
+ tags: ["auth", "sign", "in", "anilist"],
+ onClick: async () => {
+ await localforage.setItem(
+ "redirect",
+ window.location.origin +
+ window.location.pathname +
+ window.location.search,
+ );
+
+ window.location.href = loginUrl;
+ },
+ },
+ ];
+};