theo-agent-dashboard

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

auth.ts (1299B)


      1 import { create } from "zustand";
      2 import { api } from "../lib/api";
      3 
      4 interface AuthState {
      5   authenticated: boolean;
      6   configured: boolean;
      7   loading: boolean;
      8   error: string | null;
      9   login: (password: string) => Promise<void>;
     10   logout: () => Promise<void>;
     11   checkStatus: () => Promise<void>;
     12 }
     13 
     14 export const useAuthStore = create<AuthState>((set) => ({
     15   authenticated: false,
     16   configured: false,
     17   loading: false,
     18   error: null,
     19 
     20   login: async (password: string) => {
     21     set({ loading: true, error: null });
     22     try {
     23       await api.post("/auth/login", { password });
     24       set({ authenticated: true, loading: false });
     25     } catch (err) {
     26       const message =
     27         err instanceof Error ? err.message : "Login failed";
     28       set({ loading: false, error: message });
     29     }
     30   },
     31 
     32   logout: async () => {
     33     try {
     34       await api.post("/auth/logout");
     35     } finally {
     36       set({ authenticated: false });
     37     }
     38   },
     39 
     40   checkStatus: async () => {
     41     set({ loading: true });
     42     try {
     43       const data = await api.get<{ configured: boolean; authenticated: boolean }>(
     44         "/auth/status",
     45       );
     46       set({
     47         configured: data.configured,
     48         authenticated: data.authenticated,
     49         loading: false,
     50       });
     51     } catch {
     52       set({ loading: false });
     53     }
     54   },
     55 }));