Add new frontend - fix 16

This commit is contained in:
Urtzi Alfaro
2025-07-23 07:26:04 +02:00
parent e6b0be0c95
commit 1d35912459
14 changed files with 588 additions and 169 deletions

View File

@@ -97,12 +97,26 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
const register = useCallback(async (data: RegisterRequest) => {
setIsLoading(true);
try {
// Register and store tokens (like login)
const tokenResponse = await authService.register(data);
// After registration, get user profile
const profile = await authService.getCurrentUser();
setUser(profile);
// ✅ FIX: Handle registration conflicts properly
try {
// Try to register first
const tokenResponse = await authService.register(data);
// After successful registration, get user profile
const profile = await authService.getCurrentUser();
setUser(profile);
} catch (registrationError: any) {
// ✅ FIX: If user already exists (409), try to login instead
if (registrationError.response?.status === 409 ||
registrationError.message?.includes('already exists')) {
console.log('User already exists');
} else {
// If it's not a "user exists" error, re-throw it
throw registrationError;
}
}
} catch (error) {
setIsLoading(false);
throw error; // Re-throw to let components handle the error