Files
consume-rs/frontend/src/lib/auth.svelte.ts
T
2026-06-17 10:59:45 +02:00

54 lines
972 B
TypeScript

import { api } from "./api";
export type User = {
id: string;
email: string;
display_name: string | null;
email_verified: boolean;
};
export type Settings = {
locale: string;
currency: string;
theme: string;
notify_email: boolean;
};
type Me = { user: User; settings: Settings };
class AuthStore {
user = $state<User | null>(null);
settings = $state<Settings | null>(null);
loaded = $state(false);
async refresh() {
try {
const me = await api.get<Me>("/auth/me");
this.user = me.user;
this.settings = me.settings;
} catch {
this.user = null;
this.settings = null;
} finally {
this.loaded = true;
}
}
set(me: Me) {
this.user = me.user;
this.settings = me.settings;
this.loaded = true;
}
async logout() {
try {
await api.post("/auth/logout");
} finally {
this.user = null;
this.settings = null;
}
}
}
export const auth = new AuthStore();