import React, { createContext, useContext, useReducer, useEffect, ReactNode } from 'react'; import { User, LoginRequest, ApiResponse, LoginResponse } from '../types'; import { authAPI } from '../services/api'; import toast from 'react-hot-toast'; interface AuthState { user: User | null; token: string | null; isLoading: boolean; isAuthenticated: boolean; } type AuthAction = | { type: 'AUTH_START' } | { type: 'AUTH_SUCCESS'; payload: { user: User; token: string } } | { type: 'AUTH_FAILURE' } | { type: 'LOGOUT' } | { type: 'UPDATE_USER'; payload: User }; interface AuthContextType extends AuthState { login: (credentials: LoginRequest) => Promise; loginWithMyID: (credentials: LoginRequest) => Promise; logout: () => void; updateUser: (user: User) => void; updateProfile: (data: Partial) => Promise; } const AuthContext = createContext(undefined); const authReducer = (state: AuthState, action: AuthAction): AuthState => { switch (action.type) { case 'AUTH_START': return { ...state, isLoading: true }; case 'AUTH_SUCCESS': return { ...state, isLoading: false, isAuthenticated: true, user: action.payload.user, token: action.payload.token, }; case 'AUTH_FAILURE': return { ...state, isLoading: false, isAuthenticated: false, user: null, token: null, }; case 'LOGOUT': return { ...state, isAuthenticated: false, user: null, token: null, }; case 'UPDATE_USER': return { ...state, user: action.payload, }; default: return state; } }; // TEMPORARY: BYPASS AUTHENTICATION FOR DEVELOPMENT const mockUser: User = { id: '507f1f77bcf86cd799439011', ugaId: '811999999', email: 'dev@uga.edu', firstName: 'Development', lastName: 'User', role: 'student', college: 'Franklin College of Arts & Sciences', major: 'Computer Science', classification: 'senior' } as User; const initialState: AuthState = { user: mockUser, token: 'dev-bypass-token', isLoading: false, isAuthenticated: true, }; export const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => { const [state, dispatch] = useReducer(authReducer, initialState); useEffect(() => { // TEMPORARY: SKIP AUTH INITIALIZATION FOR DEVELOPMENT // Authentication is bypassed - user is always authenticated with mock data console.log('🚧 DEV MODE: Authentication bypassed'); }, []); const login = async (credentials: LoginRequest): Promise => { dispatch({ type: 'AUTH_START' }); try { const response: ApiResponse = await authAPI.login(credentials); if (response.success && response.data) { const { user, token } = response.data; localStorage.setItem('uga-chat-token', token); dispatch({ type: 'AUTH_SUCCESS', payload: { user, token } }); toast.success(`Welcome back, ${user.firstName}!`); return true; } else { dispatch({ type: 'AUTH_FAILURE' }); toast.error(response.error || 'Login failed'); return false; } } catch (error: any) { dispatch({ type: 'AUTH_FAILURE' }); toast.error(error.message || 'Login failed'); return false; } }; const loginWithMyID = async (credentials: LoginRequest): Promise => { dispatch({ type: 'AUTH_START' }); try { const response: ApiResponse = await authAPI.loginWithMyID(credentials); if (response.success && response.data) { const { user, token } = response.data; localStorage.setItem('uga-chat-token', token); dispatch({ type: 'AUTH_SUCCESS', payload: { user, token } }); toast.success(`Welcome, ${user.firstName}!`); return true; } else { dispatch({ type: 'AUTH_FAILURE' }); toast.error(response.error || 'MyID login failed'); return false; } } catch (error: any) { dispatch({ type: 'AUTH_FAILURE' }); toast.error(error.message || 'MyID login failed'); return false; } }; const logout = async () => { try { await authAPI.logout(); } catch (error) { // Continue with logout even if API call fails } finally { localStorage.removeItem('uga-chat-token'); dispatch({ type: 'LOGOUT' }); toast.success('Logged out successfully'); } }; const updateUser = (user: User) => { dispatch({ type: 'UPDATE_USER', payload: user }); }; const updateProfile = async (data: Partial): Promise => { try { const response: ApiResponse = await authAPI.updateProfile(data); if (response.success && response.data) { dispatch({ type: 'UPDATE_USER', payload: response.data }); toast.success('Profile updated successfully'); return true; } else { toast.error(response.error || 'Failed to update profile'); return false; } } catch (error: any) { toast.error(error.message || 'Failed to update profile'); return false; } }; const value: AuthContextType = { ...state, login, loginWithMyID, logout, updateUser, updateProfile, }; return {children}; }; export const useAuth = () => { const context = useContext(AuthContext); if (context === undefined) { throw new Error('useAuth must be used within an AuthProvider'); } return context; };