123 lines
3.8 KiB
TypeScript
123 lines
3.8 KiB
TypeScript
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<LoginResult | null>;
|
|
verifyTwoFactor: (challengeToken: string, code: string) => Promise<User | null>;
|
|
stopImpersonation: () => Promise<User | null>;
|
|
register: (name: string, email: string, password: string) => Promise<User | null>;
|
|
logout: () => Promise<void>;
|
|
updateCurrentUser: (user: ApiUser) => void;
|
|
isLoading: boolean;
|
|
}
|
|
|
|
const AuthContext = createContext<AuthContextType | undefined>(undefined);
|
|
|
|
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
|
const [user, setUser] = useState<User | null>(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<LoginResult | null> => {
|
|
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<User | null> => {
|
|
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<User | null> => {
|
|
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<User | null> => {
|
|
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 (
|
|
<AuthContext.Provider value={{ user, login, verifyTwoFactor, stopImpersonation, register, logout, updateCurrentUser, isLoading }}>
|
|
{children}
|
|
</AuthContext.Provider>
|
|
);
|
|
};
|
|
|
|
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;
|
|
};
|