SPB Git forge

spb/uqo-chat

Public
14commits 1branches 0releases
1.4 MBsize
maindefault branch
17 days agolast push
Python 64.6% TypeScript 33.7% CSS 0.8%
1.2 KB · 44 lines typescript
Raw Blame History
1import { create } from 'zustand';2import { api, setToken } from '@/lib/api';3import type { User } from '@/lib/types';45interface AuthState {6  user: User | null;7  loading: boolean;8  load: () => Promise<void>;9  setUser: (u: User | null) => void;10  logout: () => Promise<void>;11  updatePrefs: (p: Partial<User['preferences']> & { display_name?: string }) => Promise<void>;12}1314export const useAuth = create<AuthState>((set, get) => ({15  user: null,16  loading: true,17  load: async () => {18    try {19      const u = await api<User>('/me');20      set({ user: u, loading: false });21    } catch {22      set({ user: null, loading: false });23    }24  },25  setUser: (u) => set({ user: u, loading: false }),26  logout: async () => {27    try {28      await api('/auth/logout', { method: 'POST' });29    } finally {30      setToken(null);31      set({ user: null });32    }33  },34  updatePrefs: async (p) => {35    const u = await api<User>('/me/preferences', { method: 'PATCH', body: JSON.stringify(p) });36    set({ user: u });37    const fs = u.preferences.font_scale ?? 1;38    document.documentElement.style.setProperty('--font-scale', String(fs));39    void get;40  },41}));4243window.addEventListener('uqo:unauthorized', () => useAuth.setState({ user: null, loading: false }));44