fix session expiration

This commit is contained in:
Wavering Ana 2025-01-29 15:43:34 -05:00
parent ce9fcfbd7c
commit 8a3ee20a9d
4 changed files with 39 additions and 12 deletions

View file

@ -30,7 +30,7 @@ then check /target/release for the binary named `SimpleGit`
### From Docker ### From Docker
```bash ```bash
docker build --build-arg API_URL=http://localhost:8080 -t simplelink . docker build --build-arg API_URL=http://localhost:8080 -t simplelink .
docker run simplelink -p 8080:8080 \ docker run -p 8080:8080 \
-e JWT_SECRET=change-me-in-production \ -e JWT_SECRET=change-me-in-production \
-e DATABASE_URL=postgres://user:password@host:port/database \ -e DATABASE_URL=postgres://user:password@host:port/database \
simplelink simplelink

View file

@ -1,13 +1,16 @@
<!doctype html> <!doctype html>
<html lang="en"> <html lang="en">
<head>
<meta charset="UTF-8" /> <head>
<link rel="icon" type="image/svg+xml" href="/vite.svg" /> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="icon" type="image/svg+xml" href="/vite.svg" />
<title>Vite + React + TS</title> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head> <title>SimpleLink</title>
<body> </head>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script> <body>
</body> <div id="root"></div>
</html> <script type="module" src="/src/main.tsx"></script>
</body>
</html>

View file

@ -15,6 +15,20 @@ api.interceptors.request.use((config) => {
return 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 // Auth endpoints
export const login = async (email: string, password: string) => { export const login = async (email: string, password: string) => {
const response = await api.post<AuthResponse>('/auth/login', { const response = await api.post<AuthResponse>('/auth/login', {

View file

@ -23,6 +23,16 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
setUser(userData); setUser(userData);
} }
setIsLoading(false); setIsLoading(false);
const handleUnauthorized = () => {
setUser(null);
};
window.addEventListener('unauthorized', handleUnauthorized);
return () => {
window.removeEventListener('unauthorized', handleUnauthorized);
};
}, []); }, []);
const login = async (email: string, password: string) => { const login = async (email: string, password: string) => {