|
| 1 | +import { create } from "zustand"; |
| 2 | +import type { User } from "../types"; |
| 3 | +import { authApi } from "../api/auth.api"; |
| 4 | +import axios from "axios"; |
| 5 | + |
| 6 | +interface AuthState { |
| 7 | + user: User | null; |
| 8 | + isLoading: boolean; |
| 9 | + isCheckingAuth: boolean; |
| 10 | + error: string | null; |
| 11 | +} |
| 12 | + |
| 13 | +//Defining the interface for the store actions |
| 14 | +interface AuthActions { |
| 15 | + // Authentication methods |
| 16 | + login: (credentials: { email: string; password: string }) => Promise<void>; |
| 17 | + register: (userData: { |
| 18 | + name: string; |
| 19 | + email: string; |
| 20 | + password: string; |
| 21 | + }) => Promise<void>; |
| 22 | + logout: () => Promise<void>; |
| 23 | + |
| 24 | + // Initial check method |
| 25 | + checkAuthStatus: () => Promise<void>; |
| 26 | + |
| 27 | + //Utility.. |
| 28 | + clearError: () => void; |
| 29 | +} |
| 30 | + |
| 31 | +type AuthStore = AuthState & AuthActions; |
| 32 | + |
| 33 | +//creating a zustand store |
| 34 | + |
| 35 | +export const useAuthStore = create<AuthStore>((set) => ({ |
| 36 | + //state... |
| 37 | + user: null, |
| 38 | + isLoading: false, |
| 39 | + isCheckingAuth: true, |
| 40 | + error: null, |
| 41 | + //Actions... |
| 42 | + |
| 43 | + clearError: () => set({ error: null }), |
| 44 | + |
| 45 | + login: async (credentials) => { |
| 46 | + set({ isLoading: true, error: null }); |
| 47 | + try { |
| 48 | + const user = await authApi.login(credentials); |
| 49 | + set({ user, isLoading: false }); |
| 50 | + } catch (err) { |
| 51 | + const errorMessage = axios.isAxiosError(err) |
| 52 | + ? err.response?.data?.message || "Login failed" |
| 53 | + : "An unknown error occurred"; |
| 54 | + set({ user: null, error: errorMessage, isLoading: false }); |
| 55 | + throw err; |
| 56 | + } |
| 57 | + }, |
| 58 | + |
| 59 | + register: async (userData) => { |
| 60 | + set({ isLoading: true, error: null }); |
| 61 | + try { |
| 62 | + const user = await authApi.register(userData); |
| 63 | + set({ user, isLoading: false }); |
| 64 | + } catch (err) { |
| 65 | + const errorMessage = axios.isAxiosError(err) |
| 66 | + ? err.response?.data?.message || "Registration failed!" |
| 67 | + : "An unknown error occurred"; |
| 68 | + set({ user: null, error: errorMessage, isLoading: false }); |
| 69 | + throw err; |
| 70 | + } |
| 71 | + }, |
| 72 | + |
| 73 | + logout: async () => { |
| 74 | + set({ isLoading: true }); |
| 75 | + try { |
| 76 | + await authApi.logout(); |
| 77 | + set({ user: null, isLoading: false }); |
| 78 | + } catch { |
| 79 | + set({ user: null, isLoading: false }); |
| 80 | + } |
| 81 | + }, |
| 82 | + checkAuthStatus: async () => { |
| 83 | + set({ isCheckingAuth: true }); |
| 84 | + try { |
| 85 | + const user = await authApi.checkAuth(); |
| 86 | + set({ user, isCheckingAuth: false }); |
| 87 | + } catch { |
| 88 | + set({ user: null, isCheckingAuth: false }); |
| 89 | + } |
| 90 | + }, |
| 91 | +})); |
| 92 | + |
| 93 | +// Optional: Custom hook for cleaner component access |
| 94 | +export const useAuth = () => |
| 95 | + useAuthStore((state) => ({ |
| 96 | + user: state.user, |
| 97 | + isLoggedIn: !!state.user, |
| 98 | + isLoading: state.isLoading, |
| 99 | + isCheckingAuth: state.isCheckingAuth, |
| 100 | + error: state.error, |
| 101 | + login: state.login, |
| 102 | + logout: state.logout, |
| 103 | + register: state.register, |
| 104 | + clearError: state.clearError, |
| 105 | + })); |
0 commit comments