Initial commit
This commit is contained in:
26
app/composables/useAuth.ts
Normal file
26
app/composables/useAuth.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
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 };
|
||||
}
|
||||
Reference in New Issue
Block a user