diff --git a/README.md b/README.md index a2eac0a..595d71c 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ then check /target/release for the binary named `SimpleGit` ### From Docker ```bash 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 DATABASE_URL=postgres://user:password@host:port/database \ simplelink diff --git a/frontend/index.html b/frontend/index.html index e4b78ea..ffe5d61 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -1,13 +1,16 @@ - - - - - Vite + React + TS - - -
- - - + + + + + + SimpleLink + + + +
+ + + + \ No newline at end of file diff --git a/frontend/src/api/client.ts b/frontend/src/api/client.ts index 6f2d224..1cf80b3 100644 --- a/frontend/src/api/client.ts +++ b/frontend/src/api/client.ts @@ -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('/auth/login', { diff --git a/frontend/src/context/AuthContext.tsx b/frontend/src/context/AuthContext.tsx index a4f25db..cf5a1d8 100644 --- a/frontend/src/context/AuthContext.tsx +++ b/frontend/src/context/AuthContext.tsx @@ -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) => {