|
| 1 | +// =================== |
| 2 | +// © AngelaMos | 2025 |
| 3 | +// auth.store.ts |
| 4 | +// =================== |
| 5 | + |
| 6 | +import { create } from 'zustand' |
| 7 | +import { devtools, persist } from 'zustand/middleware' |
| 8 | +import { type UserResponse, UserRole } from '@/api/types' |
| 9 | +import { STORAGE_KEYS } from '@/config' |
| 10 | + |
| 11 | +interface AuthState { |
| 12 | + user: UserResponse | null |
| 13 | + accessToken: string | null |
| 14 | + isAuthenticated: boolean |
| 15 | + isLoading: boolean |
| 16 | +} |
| 17 | + |
| 18 | +interface AuthActions { |
| 19 | + login: (user: UserResponse, accessToken: string) => void |
| 20 | + logout: () => void |
| 21 | + setLoading: (loading: boolean) => void |
| 22 | + setAccessToken: (token: string | null) => void |
| 23 | + updateUser: (updates: Partial<UserResponse>) => void |
| 24 | +} |
| 25 | + |
| 26 | +type AuthStore = AuthState & AuthActions |
| 27 | + |
| 28 | +export const useAuthStore = create<AuthStore>()( |
| 29 | + devtools( |
| 30 | + persist( |
| 31 | + (set) => ({ |
| 32 | + user: null, |
| 33 | + accessToken: null, |
| 34 | + isAuthenticated: false, |
| 35 | + isLoading: false, |
| 36 | + |
| 37 | + login: (user, accessToken) => |
| 38 | + set( |
| 39 | + { |
| 40 | + user, |
| 41 | + accessToken, |
| 42 | + isAuthenticated: true, |
| 43 | + isLoading: false, |
| 44 | + }, |
| 45 | + false, |
| 46 | + 'auth/login' |
| 47 | + ), |
| 48 | + |
| 49 | + logout: () => |
| 50 | + set( |
| 51 | + { |
| 52 | + user: null, |
| 53 | + accessToken: null, |
| 54 | + isAuthenticated: false, |
| 55 | + isLoading: false, |
| 56 | + }, |
| 57 | + false, |
| 58 | + 'auth/logout' |
| 59 | + ), |
| 60 | + |
| 61 | + setLoading: (loading) => |
| 62 | + set({ isLoading: loading }, false, 'auth/setLoading'), |
| 63 | + |
| 64 | + setAccessToken: (token) => |
| 65 | + set({ accessToken: token }, false, 'auth/setAccessToken'), |
| 66 | + |
| 67 | + updateUser: (updates) => |
| 68 | + set( |
| 69 | + (state) => ({ |
| 70 | + user: state.user !== null ? { ...state.user, ...updates } : null, |
| 71 | + }), |
| 72 | + false, |
| 73 | + 'auth/updateUser' |
| 74 | + ), |
| 75 | + }), |
| 76 | + { |
| 77 | + name: STORAGE_KEYS.AUTH, |
| 78 | + partialize: (state) => ({ |
| 79 | + user: state.user, |
| 80 | + isAuthenticated: state.isAuthenticated, |
| 81 | + }), |
| 82 | + } |
| 83 | + ), |
| 84 | + { name: 'AuthStore' } |
| 85 | + ) |
| 86 | +) |
| 87 | + |
| 88 | +export const useUser = (): UserResponse | null => useAuthStore((s) => s.user) |
| 89 | +export const useIsAuthenticated = (): boolean => |
| 90 | + useAuthStore((s) => s.isAuthenticated) |
| 91 | +export const useIsAuthLoading = (): boolean => useAuthStore((s) => s.isLoading) |
| 92 | +export const useAccessToken = (): string | null => |
| 93 | + useAuthStore((s) => s.accessToken) |
| 94 | + |
| 95 | +export const useHasRole = (role: UserRole): boolean => { |
| 96 | + const user = useAuthStore((s) => s.user) |
| 97 | + return user !== null && user.role === role |
| 98 | +} |
| 99 | + |
| 100 | +export const useIsAdmin = (): boolean => { |
| 101 | + const user = useAuthStore((s) => s.user) |
| 102 | + return user !== null && user.role === UserRole.ADMIN |
| 103 | +} |
| 104 | + |
| 105 | +export { UserRole } |
0 commit comments