diff --git a/src/contexts/AuthContext.tsx b/src/contexts/AuthContext.tsx index 5afc4639..804d6ba8 100755 --- a/src/contexts/AuthContext.tsx +++ b/src/contexts/AuthContext.tsx @@ -7,6 +7,7 @@ interface User { username: string; email: string; role: string; + email_verified?: boolean; } interface AuthContextType { @@ -15,6 +16,7 @@ interface AuthContextType { isLoading: boolean; login: (username: string, password: string) => Promise; logout: () => void; + refreshUser: () => Promise; isAuthenticated: boolean; } @@ -139,6 +141,22 @@ export function AuthProvider({ children }: { children: ReactNode }) { } }; + const refreshUser = async () => { + if (!token) return; + const validationKey = `token_validated_${token.substring(0, 10)}`; + sessionStorage.removeItem(validationKey); + try { + const response = await authApi.getProfile(); + if (response && 'data' in response) { + setUser(response.data); + localStorage.setItem('user', JSON.stringify(response.data)); + sessionStorage.setItem(validationKey, 'true'); + } + } catch { + // silently ignore — user stays logged in + } + }; + const logout = () => { clearAuthData(); toast.info('You have been logged out'); @@ -157,7 +175,7 @@ export function AuthProvider({ children }: { children: ReactNode }) { })(); return ( - + {children} ); diff --git a/src/pages/VerifyEmail.tsx b/src/pages/VerifyEmail.tsx index b4ea984a..c5a88d79 100644 --- a/src/pages/VerifyEmail.tsx +++ b/src/pages/VerifyEmail.tsx @@ -5,12 +5,13 @@ import { useAuth } from '@/contexts/AuthContext'; import Logo from '@/components/brand/Logo'; import axios from 'axios'; + type Status = 'verifying' | 'success' | 'error'; export default function VerifyEmail() { const [searchParams] = useSearchParams(); const navigate = useNavigate(); - const { isAuthenticated } = useAuth(); + const { isAuthenticated, refreshUser } = useAuth(); const [status, setStatus] = useState('verifying'); const [message, setMessage] = useState(''); @@ -22,9 +23,10 @@ export default function VerifyEmail() { return; } axios.post('/api/auth/verify-email', { token }) - .then(res => { + .then(async res => { setStatus('success'); setMessage(res.data.message || 'Email verified successfully!'); + await refreshUser(); setTimeout(() => navigate('/dashboard'), 3000); }) .catch(err => {