Fix React error #130 - Remove authentication race condition

- Remove setTimeout delays in AdminAuthContext login function
- Make authentication state updates synchronous
- Replace setTimeout navigation with state-based navigation in AdminLoginPage
- Add proper error handling and component lifecycle management in CreateEventPage
- Prevent navigation if component unmounts during async operations

This fixes the issue where users would see React error #130 during login
and couldn't create events or save settings.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-07-07 13:12:39 +02:00
parent 9dd643338b
commit 2e10374e2c
3 changed files with 40 additions and 26 deletions
+6 -11
View File
@@ -1,5 +1,5 @@
import React, { useState } from 'react';
import { useNavigate, Navigate } from 'react-router-dom';
import { Navigate } from 'react-router-dom';
import { Lock, Mail, Eye, EyeOff, AlertCircle } from 'lucide-react';
import { toast } from 'react-toastify';
@@ -9,7 +9,6 @@ import { authService } from '../../services/auth.service';
import { getAuthToken } from '../../config/api';
export const AdminLoginPage: React.FC = () => {
const navigate = useNavigate();
const { isAuthenticated, login } = useAdminAuth();
const [formData, setFormData] = useState({
@@ -19,9 +18,10 @@ export const AdminLoginPage: React.FC = () => {
const [showPassword, setShowPassword] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [errors, setErrors] = useState<Record<string, string>>({});
const [loginSuccess, setLoginSuccess] = useState(false);
// Redirect if already authenticated
if (isAuthenticated) {
// Redirect if already authenticated or login successful
if (isAuthenticated || loginSuccess) {
return <Navigate to="/admin/dashboard" replace />;
}
@@ -58,10 +58,7 @@ export const AdminLoginPage: React.FC = () => {
const response = await authService.adminLogin(formData);
login(response.token, response.user);
toast.success('Login successful!');
// Add a small delay to ensure auth state is updated
setTimeout(() => {
navigate('/admin/dashboard');
}, 200);
setLoginSuccess(true);
} catch (error: any) {
console.error('Login error:', error);
@@ -71,9 +68,7 @@ export const AdminLoginPage: React.FC = () => {
const token = getAuthToken(true);
if (token) {
// Login was successful, just had a connection issue
setTimeout(() => {
navigate('/admin/dashboard');
}, 200);
setLoginSuccess(true);
return;
}
toast.error('Network error. Please check your connection and try again.');