fix session expiration
This commit is contained in:
parent
ce9fcfbd7c
commit
8a3ee20a9d
4 changed files with 39 additions and 12 deletions
|
@ -15,6 +15,20 @@ api.interceptors.request.use((config) => {
|
|||
return config;
|
||||
});
|
||||
|
||||
api.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
if (error.response?.status === 401) {
|
||||
localStorage.removeItem('token');
|
||||
localStorage.removeItem('user');
|
||||
|
||||
window.dispatchEvent(new Event('unauthorized'));
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
// Auth endpoints
|
||||
export const login = async (email: string, password: string) => {
|
||||
const response = await api.post<AuthResponse>('/auth/login', {
|
||||
|
|
|
@ -23,6 +23,16 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
|||
setUser(userData);
|
||||
}
|
||||
setIsLoading(false);
|
||||
|
||||
const handleUnauthorized = () => {
|
||||
setUser(null);
|
||||
};
|
||||
|
||||
window.addEventListener('unauthorized', handleUnauthorized);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('unauthorized', handleUnauthorized);
|
||||
};
|
||||
}, []);
|
||||
|
||||
const login = async (email: string, password: string) => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue