diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 8328494..32da70f 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -1,5 +1,5 @@ import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom'; -import { ThemeProvider, createTheme, CssBaseline } from '@mui/material'; +import { ThemeProvider, createTheme, CssBaseline, Box, CircularProgress } from '@mui/material'; import { AuthProvider, useAuth } from './context/AuthContext'; import Layout from './components/Layout/Layout'; import Login from './pages/Login'; @@ -30,7 +30,16 @@ function PrivateRoute({ children }) { const { user, loading } = useAuth(); if (loading) { - return null; + return ( + + + + ); } return user ? children : ; diff --git a/frontend/src/context/AuthContext.jsx b/frontend/src/context/AuthContext.jsx index 882c894..5b8964b 100644 --- a/frontend/src/context/AuthContext.jsx +++ b/frontend/src/context/AuthContext.jsx @@ -16,9 +16,12 @@ export const AuthProvider = ({ children }) => { const response = await authService.checkAuth(); if (response.authenticated) { setUser(response.user); + } else { + setUser(null); } } catch (error) { console.error('Auth check failed:', error); + setUser(null); } finally { setLoading(false); } diff --git a/frontend/src/services/api.js b/frontend/src/services/api.js index 1a2b768..ab92622 100644 --- a/frontend/src/services/api.js +++ b/frontend/src/services/api.js @@ -14,10 +14,8 @@ const api = axios.create({ api.interceptors.response.use( (response) => response, (error) => { - if (error.response?.status === 401) { - // Redirect to login if unauthorized - window.location.href = '/login'; - } + // Don't redirect here - let React Router handle it + // 401 errors will be caught by components and Navigate will handle redirect return Promise.reject(error); } );