Files
quickdrop/app/composables/useAuth.ts
2026-08-07 22:13:25 -05:00

27 lines
799 B
TypeScript

export function useAuth() {
const authenticated = useState("quickdrop-authenticated", () => false);
const session = useFetch<{ authenticated: boolean }>("/api/auth/session", {
key: "quickdrop-auth-session",
immediate: false,
});
async function refresh() {
try {
await session.execute();
authenticated.value =
!session.error.value && session.data.value?.authenticated === true;
} catch {
authenticated.value = false;
}
}
async function login(passcode: string) {
await $fetch("/api/auth/login", { method: "POST", body: { passcode } });
await refresh();
}
async function logout() {
await $fetch("/api/auth/logout", { method: "POST" });
authenticated.value = false;
}
return { authenticated, refresh, login, logout };
}