| 1 | import React, { createContext, useContext, useEffect, useState } from 'react';
|
| 2 | import { apiClient, User } from '@/lib/api';
|
| 3 |
|
| 4 | interface AuthProviderProps {
|
| 5 | children: React.ReactNode;
|
| 6 | }
|
| 7 |
|
| 8 | interface AuthContextType {
|
| 9 | user: User | null;
|
| 10 | isLoading: boolean;
|
| 11 | isAuthenticated: boolean;
|
| 12 | updateAuthUser: (user: {
|
| 13 | firstName: string;
|
| 14 | lastName?: string;
|
| 15 | email: string;
|
| 16 | updatedAt: date;
|
| 17 | }) => void;
|
| 18 | login: (email: string, password: string) => Promise<void>;
|
| 19 | signup: (userData: {
|
| 20 | firstName: string;
|
| 21 | lastName?: string;
|
| 22 | email: string;
|
| 23 | password: string;
|
| 24 | }) => Promise<void>;
|
| 25 | logout: () => Promise<void>;
|
| 26 | forgotPassword: (email: string) => Promise<{ message: string; token?: string }>;
|
| 27 | resetPassword: (token: string, password: string) => Promise<void>;
|
| 28 | }
|
| 29 |
|
| 30 | const AuthContext = createContext<AuthContextType | null>(null);
|
| 31 |
|
| 32 | export function useAuth() {
|
| 33 | const context = useContext(AuthContext);
|
| 34 | if (!context) {
|
| 35 | throw new Error('useAuth must be used within an AuthProvider');
|
| 36 | }
|
| 37 |
|
| 38 | return context;
|
| 39 | }
|
| 40 |
|
| 41 | export const AuthProvider = ({ children }: AuthProviderProps) => {
|
| 42 | const [user, setUser] = useState<User | null>(null);
|
| 43 | const [isLoading, setIsLoading] = useState(true);
|
| 44 |
|
| 45 | const isAuthenticated = !!user;
|
| 46 |
|
| 47 | // Check for existing authentication on app start
|
| 48 | useEffect(() => {
|
| 49 | checkAuthState();
|
| 50 | }, []);
|
| 51 |
|
| 52 | const checkAuthState = async () => {
|
| 53 | try {
|
| 54 | const { token } = await apiClient.getStoredAuthData();
|
| 55 |
|
| 56 | if (token) {
|
| 57 | const verifyResponse = await apiClient.verifyToken();
|
| 58 | if (verifyResponse.valid && verifyResponse.userId && verifyResponse.email) {
|
| 59 | // TODO: Fetch the full user profile
|
| 60 | // For now, we'll create a basic user object
|
| 61 | setUser({
|
| 62 | id: verifyResponse.userId,
|
| 63 | email: verifyResponse.email,
|
| 64 | firstName: '',
|
| 65 | lastName: undefined,
|
| 66 | });
|
| 67 | }
|
| 68 | }
|
| 69 | } catch (error) {
|
| 70 | console.error('Auth check failed:', error);
|
| 71 | // Token is invalid, wipe out any stored data
|
| 72 | await apiClient.logout();
|
| 73 | } finally {
|
| 74 | setIsLoading(false);
|
| 75 | }
|
| 76 | };
|
| 77 |
|
| 78 | const login = async (email: string, password: string) => {
|
| 79 | setIsLoading(true);
|
| 80 |
|
| 81 | try {
|
| 82 | const response = await apiClient.login(email, password);
|
| 83 | setUser(response.user);
|
| 84 | } finally {
|
| 85 | setIsLoading(false);
|
| 86 | }
|
| 87 | };
|
| 88 |
|
| 89 | const signup = async (userData: {
|
| 90 | firstName: string;
|
| 91 | lastName?: string;
|
| 92 | email: string;
|
| 93 | password: string;
|
| 94 | }) => {
|
| 95 | setIsLoading(true);
|
| 96 | try {
|
| 97 | const response = await apiClient.signup(userData);
|
| 98 | setUser(response.user);
|
| 99 | } finally {
|
| 100 | setIsLoading(false);
|
| 101 | }
|
| 102 | };
|
| 103 |
|
| 104 | const logout = async () => {
|
| 105 | setIsLoading(true);
|
| 106 | try {
|
| 107 | await apiClient.logout();
|
| 108 | setUser(null);
|
| 109 | } finally {
|
| 110 | setIsLoading(false);
|
| 111 | }
|
| 112 | };
|
| 113 |
|
| 114 | const forgotPassword = async (email: string) => {
|
| 115 | return apiClient.forgotPassword(email);
|
| 116 | };
|
| 117 |
|
| 118 | const resetPassword = async (token: string, password: string) => {
|
| 119 | await apiClient.resetPassword(token, password);
|
| 120 | };
|
| 121 |
|
| 122 | const updateAuthUser = (user) => {
|
| 123 | setUser(user);
|
| 124 | }
|
| 125 |
|
| 126 | const value: AuthContextType = {
|
| 127 | user,
|
| 128 | isLoading,
|
| 129 | isAuthenticated,
|
| 130 | login,
|
| 131 | signup,
|
| 132 | logout,
|
| 133 | forgotPassword,
|
| 134 | resetPassword,
|
| 135 | updateAuthUser
|
| 136 | };
|
| 137 |
|
| 138 | return (
|
| 139 | <AuthContext.Provider value={value}>
|
| 140 | {children}
|
| 141 | </AuthContext.Provider>
|
| 142 | );
|
| 143 | }
|