Add new frontend - fix 16
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user