Fix jwt-token
This commit is contained in:
@@ -66,6 +66,16 @@ export const AuthProvider = ({ children }) => {
|
||||
}
|
||||
}, []);
|
||||
|
||||
// Listen for auth logout events from API interceptor
|
||||
useEffect(() => {
|
||||
const handleAuthLogout = () => {
|
||||
dispatch({ type: 'LOGOUT' });
|
||||
};
|
||||
|
||||
window.addEventListener('auth-logout', handleAuthLogout);
|
||||
return () => window.removeEventListener('auth-logout', handleAuthLogout);
|
||||
}, []);
|
||||
|
||||
const checkAuthStatus = async () => {
|
||||
try {
|
||||
dispatch({ type: 'SET_LOADING', payload: true });
|
||||
|
||||
@@ -46,12 +46,10 @@ api.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
if (error.response?.status === 401) {
|
||||
// Token expired or invalid - let AuthContext handle this
|
||||
// Token expired or invalid - remove token and let ProtectedRoute handle navigation
|
||||
localStorage.removeItem('token');
|
||||
// Only redirect if not already on login page
|
||||
if (window.location.pathname !== '/login') {
|
||||
window.location.href = '/login';
|
||||
}
|
||||
// Force a state update by dispatching a custom event
|
||||
window.dispatchEvent(new CustomEvent('auth-logout'));
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user