aboutsummaryrefslogtreecommitdiff
path: root/src/lib/CommandPalette/authActions.ts
blob: c306eb34f1f00447ee9be63ed862da1b7d036326 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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;
			},
		},
	];
};