Files
Webka/frontend/stores/session.ts
2026-09-06 16:23:09 +05:00

32 lines
725 B
TypeScript

import { defineStore } from "pinia";
export interface User {
id: string;
username: string;
role: "admin" | "viewer";
session_id: string;
}
export const useSessionStore = defineStore("session", () => {
const user = ref<User | null>(null);
const ready = ref(false);
async function refresh() {
try {
user.value = await $fetch<User>("/api/session");
return true;
} catch {
user.value = null;
return false;
} finally {
ready.value = true;
}
}
async function logout() {
try {
await $fetch("/api/logout", { method: "POST" });
} finally {
user.value = null;
await navigateTo("/login");
}
}
return { user, ready, refresh, logout };
});