Files
headquarter/apps/web/src/stores/auth.ts
T
2026-05-16 14:57:55 +00:00

28 lines
810 B
TypeScript

import { create } from 'zustand'
import type { User } from '../types/api.ts'
export type AuthState = 'loading' | 'authenticated' | 'unauthenticated' | 'error'
interface AuthStore {
state: AuthState
user: User | null
error: Error | null
setState: (state: AuthState) => void
setUser: (user: User | null) => void
setError: (error: Error | null) => void
logout: () => void
}
export const useAuthStore = create<AuthStore>((set) => ({
state: 'loading',
user: null,
error: null,
setState: (state) => set({ state }),
setUser: (user) => set({ user, state: user ? 'authenticated' : 'unauthenticated' }),
setError: (error) => set({ error, state: 'error' }),
logout: () => {
localStorage.removeItem('access_token')
set({ user: null, state: 'unauthenticated', error: null })
},
}))