import { defineStore } from "pinia"; import { ref } from "vue"; import { api } from "../api/client"; export interface User { id: string; email: string; display_name: string; email_verified: boolean; is_admin: boolean; } export const useSessionStore = defineStore("session", () => { const user = ref(null); // Whether we've resolved the initial /me check yet (guards against redirect flashes). const loaded = ref(false); async function fetchMe(): Promise { try { user.value = await api.get("/api/auth/me"); } catch { user.value = null; } finally { loaded.value = true; } } async function login(email: string, password: string): Promise { user.value = await api.post("/api/auth/login", { email, password }); } async function register(email: string, password: string, displayName: string): Promise { user.value = await api.post("/api/auth/register", { email, password, display_name: displayName, }); } async function logout(): Promise { await api.post("/api/auth/logout"); user.value = null; } return { user, loaded, fetchMe, login, register, logout }; });