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 () => {
|
const checkAuthStatus = async () => {
|
||||||
try {
|
try {
|
||||||
dispatch({ type: 'SET_LOADING', payload: true });
|
dispatch({ type: 'SET_LOADING', payload: true });
|
||||||
|
|||||||
@@ -46,12 +46,10 @@ api.interceptors.response.use(
|
|||||||
(response) => response,
|
(response) => response,
|
||||||
(error) => {
|
(error) => {
|
||||||
if (error.response?.status === 401) {
|
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');
|
localStorage.removeItem('token');
|
||||||
// Only redirect if not already on login page
|
// Force a state update by dispatching a custom event
|
||||||
if (window.location.pathname !== '/login') {
|
window.dispatchEvent(new CustomEvent('auth-logout'));
|
||||||
window.location.href = '/login';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user