first commit
Some checks failed
CI / Validate frontend and API (push) Failing after 7m27s

This commit is contained in:
Cauê Faleiros
2026-08-31 12:38:34 -03:00
parent 684f929f28
commit 701788ea08
40 changed files with 5396 additions and 380 deletions

View File

@@ -1,9 +1,11 @@
import React, { createContext, useContext, useState, useEffect } from 'react';
import { User, UserRole } from '../types';
import { authApi, clearSession, getSession, saveSession } from '../services/api';
interface AuthContextType {
user: User | null;
login: (email: string) => boolean;
login: (email: string, password: string) => Promise<User | null>;
register: (name: string, email: string, password: string) => Promise<User | null>;
logout: () => void;
isLoading: boolean;
}
@@ -15,55 +17,73 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
// Check localStorage on mount with error handling
const storedUser = localStorage.getItem('educlean_user');
if (storedUser) {
try {
const parsedUser = JSON.parse(storedUser);
if (parsedUser && parsedUser.email) {
setUser(parsedUser);
}
} catch (error) {
console.error("Failed to parse user session, clearing storage:", error);
localStorage.removeItem('educlean_user');
const restoreSession = async () => {
const session = getSession();
if (!session) {
setIsLoading(false);
return;
}
}
setIsLoading(false);
try {
const { user: apiUser } = await authApi.me();
const restoredUser = toFrontendUser(apiUser);
saveSession({ ...session, user: apiUser });
setUser(restoredUser);
} catch {
clearSession();
} finally {
setIsLoading(false);
}
};
void restoreSession();
}, []);
const login = (email: string): boolean => {
let newUser: User | null = null;
if (email === 'admin@prof.com') {
newUser = { email, name: 'Prof. Anderson', role: 'professor' };
} else if (email === 'aluno@test.com') {
newUser = { email, name: 'Aluno Demo', role: 'student' };
const login = async (email: string, password: string): Promise<User | null> => {
try {
const session = await authApi.login(email, password);
const signedInUser = toFrontendUser(session.user);
saveSession(session);
setUser(signedInUser);
return signedInUser;
} catch {
return null;
}
};
if (newUser) {
setUser(newUser);
localStorage.setItem('educlean_user', JSON.stringify(newUser));
return true;
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);
setUser(signedInUser);
return signedInUser;
} catch {
return null;
}
return false;
};
const logout = () => {
setUser(null);
localStorage.removeItem('educlean_user');
clearSession();
};
return (
<AuthContext.Provider value={{ user, login, logout, isLoading }}>
<AuthContext.Provider value={{ user, login, register, logout, isLoading }}>
{children}
</AuthContext.Provider>
);
};
const toFrontendUser = (user: { email: string; name: string; role: string }): User => ({
email: user.email,
name: user.name,
role: user.role === 'student' ? 'student' : 'professor',
});
export const useAuth = () => {
const context = useContext(AuthContext);
if (context === undefined) {
throw new Error('useAuth must be used within an AuthProvider');
}
return context;
};
};