Enhance API clients with auto-logout on unauthorized responses; improve navigation and schedule handling with better error management and UI updates
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
+28
-4
@@ -16,7 +16,31 @@ const AUTH_API = normalizeApiBaseUrl(RUNTIME.AUTH_API || import.meta.env.VITE_AU
|
||||
const TRANSIT_API = normalizeApiBaseUrl(RUNTIME.TRANSIT_API || import.meta.env.VITE_TRANSIT_API, 'http://localhost:5001')
|
||||
|
||||
export const authClient = axios.create({ baseURL: AUTH_API })
|
||||
export const transitClient = (token) => axios.create({
|
||||
baseURL: TRANSIT_API,
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
})
|
||||
|
||||
export const transitClient = (token) => {
|
||||
const client = axios.create({
|
||||
baseURL: TRANSIT_API,
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
})
|
||||
|
||||
// Auto-logout on unauthorized responses to avoid delayed failures
|
||||
client.interceptors.response.use(
|
||||
response => response,
|
||||
error => {
|
||||
try {
|
||||
const status = error?.response?.status
|
||||
if (status === 401 || status === 403) {
|
||||
localStorage.removeItem('token')
|
||||
localStorage.removeItem('user')
|
||||
// reload to let App show the Login screen
|
||||
if (typeof window !== 'undefined') window.location.reload()
|
||||
}
|
||||
} catch (e) {
|
||||
// ignore
|
||||
}
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user