feat: add 2FA, secure cookies and publishing tools
This commit is contained in:
@@ -1,12 +1,18 @@
|
||||
import React, { createContext, useContext, useState, useEffect } from 'react';
|
||||
import { User, UserRole } from '../types';
|
||||
import { ApiUser, authApi, clearSession, getSession, saveSession } from '../services/api';
|
||||
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<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: () => void;
|
||||
logout: () => Promise<void>;
|
||||
updateCurrentUser: (user: ApiUser) => void;
|
||||
isLoading: boolean;
|
||||
}
|
||||
@@ -19,17 +25,10 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
||||
|
||||
useEffect(() => {
|
||||
const restoreSession = async () => {
|
||||
const session = getSession();
|
||||
if (!session) {
|
||||
setIsLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const { user: apiUser } = await authApi.me();
|
||||
const restoredUser = toFrontendUser(apiUser);
|
||||
saveSession({ ...session, user: apiUser });
|
||||
setUser(restoredUser);
|
||||
setUser(toFrontendUser(apiUser));
|
||||
saveSession({ user: apiUser });
|
||||
} catch {
|
||||
clearSession();
|
||||
} finally {
|
||||
@@ -39,11 +38,24 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
||||
void restoreSession();
|
||||
}, []);
|
||||
|
||||
const login = async (email: string, password: string): Promise<User | null> => {
|
||||
const login = async (email: string, password: string): Promise<LoginResult | null> => {
|
||||
try {
|
||||
const session = await authApi.login(email, password);
|
||||
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(session);
|
||||
saveSession({ user: session.user });
|
||||
setUser(signedInUser);
|
||||
return signedInUser;
|
||||
} catch {
|
||||
@@ -51,11 +63,23 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
||||
}
|
||||
};
|
||||
|
||||
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(session);
|
||||
saveSession({ user: session.user });
|
||||
setUser(signedInUser);
|
||||
return signedInUser;
|
||||
} catch {
|
||||
@@ -63,29 +87,30 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
||||
}
|
||||
};
|
||||
|
||||
const logout = () => {
|
||||
const logout = async () => {
|
||||
await authApi.logout().catch(() => undefined);
|
||||
setUser(null);
|
||||
clearSession();
|
||||
};
|
||||
|
||||
const updateCurrentUser = (apiUser: ApiUser) => {
|
||||
const session = getSession();
|
||||
if (session) saveSession({ ...session, user: apiUser });
|
||||
saveSession({ user: apiUser });
|
||||
setUser(toFrontendUser(apiUser));
|
||||
};
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{ user, login, register, logout, updateCurrentUser, isLoading }}>
|
||||
<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 }): User => ({
|
||||
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 = () => {
|
||||
|
||||
Reference in New Issue
Block a user