Files
xevion.dev/web/src/lib/stores/auth.svelte.ts
Xevion d360f2284e feat: add PostHog telemetry with type-safe event tracking
Track page views, project interactions, theme changes, external links, PGP actions, and errors. Console logging in dev when PostHog not configured. Requires PUBLIC_POSTHOG_KEY and PUBLIC_POSTHOG_HOST env vars.
2026-01-14 12:25:10 -06:00

76 lines
1.7 KiB
TypeScript

import { telemetry } from "$lib/telemetry";
class AuthStore {
isAuthenticated = $state(false);
username = $state<string | null>(null);
async login(username: string, password: string): Promise<boolean> {
try {
const response = await fetch("/api/login", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ username, password }),
credentials: "include",
});
if (response.ok) {
const data = await response.json();
this.isAuthenticated = true;
this.username = data.username;
telemetry.identifyAdmin(data.username);
return true;
}
return false;
} catch (error) {
console.error("Login error:", error);
return false;
}
}
async logout(): Promise<void> {
try {
await fetch("/api/logout", {
method: "POST",
credentials: "include",
});
} catch (error) {
console.error("Logout error:", error);
} finally {
this.isAuthenticated = false;
this.username = null;
telemetry.reset();
}
}
async checkSession(): Promise<boolean> {
try {
const response = await fetch("/api/session", {
credentials: "include",
});
if (response.ok) {
const session = await response.json();
this.isAuthenticated = true;
this.username = session.username;
return true;
}
} catch (error) {
console.error("Session check error:", error);
}
this.isAuthenticated = false;
this.username = null;
return false;
}
setSession(username: string): void {
this.isAuthenticated = true;
this.username = username;
}
}
export const authStore = new AuthStore();