import { create } from 'zustand'; import { persist } from 'zustand/middleware'; interface User { id: string; email: string; name: string; workspaceId: string; role: 'admin' | 'member'; } interface AuthState { user: User | null; token: string | null; isAuthenticated: boolean; login: (email: string, password: string) => Promise; logout: () => void; signup: (email: string, password: string, name: string) => Promise; } export const useAuthStore = create()( persist( (set) => ({ user: null, token: null, isAuthenticated: false, login: async (email: string, password: string) => { const res = await fetch('/api/auth', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password }), }); if (!res.ok) { const err = await res.json().catch(() => ({})); throw new Error((err as any).error || 'Login failed'); } const { token, user } = await res.json(); set({ user, token, isAuthenticated: true }); }, logout: () => { set({ user: null, token: null, isAuthenticated: false, }); }, signup: async (email: string, password: string, name: string) => { const res = await fetch('/api/auth', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password, name }), }); if (!res.ok) { const err = await res.json().catch(() => ({})); throw new Error((err as any).error || 'Signup failed'); } const { token, user } = await res.json(); set({ user, token, isAuthenticated: true }); }, }), { name: 'auth-storage', } ) );