import React, { createContext, useContext, useState, useEffect } from 'react'; import { User } from '../types'; import { ApiUser, authApi, clearSession, saveSession } from '../services/api'; export type LoginResult = | { user: User } | { twoFactorRequired: true; challengeToken: string }; interface AuthContextType { user: User | null; login: (email: string, password: string) => Promise; verifyTwoFactor: (challengeToken: string, code: string) => Promise; stopImpersonation: () => Promise; register: (name: string, email: string, password: string) => Promise; logout: () => Promise; updateCurrentUser: (user: ApiUser) => void; isLoading: boolean; } const AuthContext = createContext(undefined); export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { const [user, setUser] = useState(null); const [isLoading, setIsLoading] = useState(true); useEffect(() => { const restoreSession = async () => { try { const { user: apiUser } = await authApi.me(); setUser(toFrontendUser(apiUser)); saveSession({ user: apiUser }); } catch { clearSession(); } finally { setIsLoading(false); } }; void restoreSession(); }, []); const login = async (email: string, password: string): Promise => { try { const response = await authApi.login(email, password); if ('twoFactorRequired' in response) return response; const signedInUser = toFrontendUser(response.user); saveSession({ user: response.user }); setUser(signedInUser); return { user: signedInUser }; } catch { return null; } }; const verifyTwoFactor = async (challengeToken: string, code: string): Promise => { try { const session = await authApi.verifyTwoFactor(challengeToken, code); const signedInUser = toFrontendUser(session.user); saveSession({ user: session.user }); setUser(signedInUser); return signedInUser; } catch { return null; } }; const stopImpersonation = async (): Promise => { try { const response = await authApi.stopImpersonation(); const restored = toFrontendUser(response.user); saveSession({ user: response.user }); setUser(restored); return restored; } catch { return null; } }; const register = async (name: string, email: string, password: string): Promise => { try { const session = await authApi.register(name, email, password); const signedInUser = toFrontendUser(session.user); saveSession({ user: session.user }); setUser(signedInUser); return signedInUser; } catch { return null; } }; const logout = async () => { await authApi.logout().catch(() => undefined); setUser(null); clearSession(); }; const updateCurrentUser = (apiUser: ApiUser) => { saveSession({ user: apiUser }); setUser(toFrontendUser(apiUser)); }; return ( {children} ); }; const toFrontendUser = (user: { id: string; email: string; name: string; role: string; impersonatedBy?: string }): User => ({ id: user.id, email: user.email, name: user.name, role: user.role === 'admin' ? 'superadmin' : user.role === 'instructor' ? 'professor' : 'student', impersonatedBy: user.impersonatedBy, }); export const useAuth = () => { const context = useContext(AuthContext); if (context === undefined) { throw new Error('useAuth must be used within an AuthProvider'); } return context; };