all repos — stealth-developers @ 7c56a6b32a7cd20904cba12b5dae1cbe15dbbb4b

frontend/src/stores/auth.ts (view raw)

 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
import { ref } from "vue";
import { defineStore } from "pinia";

import type { RESTGetAPIGuildMemberResult } from "discord-api-types/rest/v10";

export const useAuthStore = defineStore("auth", () => {
	const isAuthenticated = ref(false);
	const user = ref<RESTGetAPIGuildMemberResult | null>(null);

	async function getUser() {
		try {
			const response = await fetch("/api/me");
			const data = await response.json();
			user.value = data.profile;
			isAuthenticated.value = true;
		} catch {
			window.location.href = "/api/login";
		}
	}

	return {
		isAuthenticated,
		user,
		getUser,
	};
});