Add new frontend - fix 9

This commit is contained in:
Urtzi Alfaro
2025-07-22 17:01:12 +02:00
parent 5959eb6e15
commit 06cbe3f4e8
16 changed files with 2048 additions and 166 deletions

View File

@@ -1,8 +1,14 @@
// frontend/src/contexts/AuthContext.tsx - UPDATED TO USE NEW REGISTRATION FLOW
import React, { createContext, useContext, useEffect, useState, useCallback } from 'react';
import { authService, UserProfile, RegisterData } from '../api/services/authService';
import { authService } from '../api/services/authService';
import { tokenManager } from '../api/auth/tokenManager';
import {
UserProfile
} from '@/api/services';
import api from '@/api/services';
interface AuthContextType {
user: UserProfile | null;
isAuthenticated: boolean;
@@ -34,7 +40,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
try {
await tokenManager.initialize();
if (authService.isAuthenticated()) {
if (api.auth.isAuthenticated()) {
// Get user from token first (faster), then validate with API
const tokenUser = tokenManager.getUserFromToken();
if (tokenUser) {
@@ -50,7 +56,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
// Validate with API and get complete profile
try {
const profile = await authService.getCurrentUser();
const profile = await api.auth.getCurrentUser();
setUser(profile);
} catch (error) {
console.error('Failed to fetch user profile:', error);
@@ -124,10 +130,10 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
}, [user]);
const refreshUser = useCallback(async () => {
if (!authService.isAuthenticated()) return;
if (!api.auth.isAuthenticated()) return;
try {
const profile = await authService.getCurrentUser();
const profile = await api.auth.getCurrentUser();
setUser(profile);
} catch (error) {
console.error('User refresh error:', error);
@@ -160,7 +166,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
if (!user) return;
const checkTokenValidity = () => {
if (!authService.isAuthenticated()) {
if (!api.auth.isAuthenticated()) {
console.warn('Token became invalid, logging out user');
logout();
}
@@ -173,7 +179,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
const contextValue = {
user,
isAuthenticated: !!user && authService.isAuthenticated(),
isAuthenticated: !!user && api.auth.isAuthenticated(),
isLoading,
login,
register,