fix: resolve login persistence bug and aggressive logout on network blips
All checks were successful
Build and Deploy / build-and-push (push) Successful in 1m58s
All checks were successful
Build and Deploy / build-and-push (push) Successful in 1m58s
- Updated Login.tsx to automatically redirect users to the dashboard if a valid token is already present in localStorage. - Refactored getUserById to properly throw server/network errors instead of silently returning undefined. - Updated AuthGuard in App.tsx to gracefully handle network errors without destroying the user's valid localStorage tokens.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import React, { useState } from 'react';
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { useNavigate, Link } from 'react-router-dom';
|
||||
import { Hexagon, Lock, Mail, ArrowRight, Loader2, Eye, EyeOff, AlertCircle } from 'lucide-react';
|
||||
import { login, logout } from '../services/dataService';
|
||||
@@ -12,6 +12,16 @@ export const Login: React.FC = () => {
|
||||
const [error, setError] = useState('');
|
||||
const [emailError, setEmailError] = useState('');
|
||||
|
||||
// Auto-redirect if already logged in
|
||||
useEffect(() => {
|
||||
const token = localStorage.getItem('ctms_token');
|
||||
const userId = localStorage.getItem('ctms_user_id');
|
||||
if (token && userId && token !== 'undefined' && token !== 'null') {
|
||||
// Opt to send them to root, AuthGuard will handle role-based routing
|
||||
navigate('/');
|
||||
}
|
||||
}, [navigate]);
|
||||
|
||||
const validateEmail = (val: string) => {
|
||||
setEmail(val);
|
||||
if (!val) {
|
||||
|
||||
Reference in New Issue
Block a user