Add new frontend - fix 12

This commit is contained in:
Urtzi Alfaro
2025-07-22 19:40:12 +02:00
parent 6717ce7e0d
commit 5dffe39706
5 changed files with 65 additions and 680 deletions

View File

@@ -1,20 +1,19 @@
// frontend/src/contexts/AuthContext.tsx - UPDATED TO USE NEW REGISTRATION FLOW
// frontend/src/contexts/AuthContext.tsx - FIXED VERSION
import React, { createContext, useContext, useEffect, useState, useCallback } from 'react';
import { authService } from '../api/services/authService';
import { tokenManager } from '../api/auth/tokenManager';
import {
UserProfile
} from '@/api/services';
import api from '@/api/services';
UserProfile,
RegisterRequest,
api
} from '../api/services';
interface AuthContextType {
user: UserProfile | null;
isAuthenticated: boolean;
isLoading: boolean;
login: (email: string, password: string) => Promise<void>;
register: (data: RegisterData) => Promise<void>; // SIMPLIFIED - no longer needs auto-login
register: (data: RegisterRequest) => Promise<void>; // FIXED: Use RegisterRequest
logout: () => Promise<void>;
updateProfile: (updates: Partial<UserProfile>) => Promise<void>;
refreshUser: () => Promise<void>;
@@ -50,6 +49,9 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
full_name: tokenUser.full_name,
is_active: true,
is_verified: tokenUser.is_verified,
role: 'user', // Default role
language: 'es',
timezone: 'Europe/Madrid',
created_at: '', // Will be filled by API call
});
}
@@ -78,7 +80,11 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
const login = useCallback(async (email: string, password: string) => {
setIsLoading(true);
try {
const profile = await authService.login({ email, password });
// Login and store tokens
const tokenResponse = await authService.login({ email, password });
// After login, get user profile
const profile = await api.auth.getCurrentUser();
setUser(profile);
} catch (error) {
setIsLoading(false);
@@ -88,10 +94,9 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
}
}, []);
const register = useCallback(async (data: RegisterData) => {
const register = useCallback(async (data: RegisterRequest) => {
setIsLoading(true);
try {
// NEW: Registration now handles tokens internally - no auto-login needed!
const profile = await authService.register(data);
setUser(profile);
} catch (error) {
@@ -193,4 +198,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
{children}
</AuthContext.Provider>
);
};
};
// Export the RegisterRequest type for use in components
export type { RegisterRequest };