Spaces:
Running
Running
Commit
·
0e57db8
1
Parent(s):
96a9109
Fix: Handle 401 errors with automatic redirect to login
Browse files- Add 401 (Unauthorized) handling in fetchApi
- Automatically redirect to login page when session expires
- Add credentials: "include" to ensure cookies are sent
- Update .gitignore to not ignore frontend/src/lib/
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- .gitignore +2 -0
- frontend/src/lib/api.ts +9 -0
.gitignore
CHANGED
|
@@ -12,6 +12,8 @@ eggs/
|
|
| 12 |
.eggs/
|
| 13 |
lib/
|
| 14 |
lib64/
|
|
|
|
|
|
|
| 15 |
parts/
|
| 16 |
sdist/
|
| 17 |
var/
|
|
|
|
| 12 |
.eggs/
|
| 13 |
lib/
|
| 14 |
lib64/
|
| 15 |
+
# But don't ignore frontend source lib
|
| 16 |
+
!frontend/src/lib/
|
| 17 |
parts/
|
| 18 |
sdist/
|
| 19 |
var/
|
frontend/src/lib/api.ts
CHANGED
|
@@ -30,10 +30,19 @@ async function fetchApi<T>(
|
|
| 30 |
"Content-Type": "application/json",
|
| 31 |
...options?.headers,
|
| 32 |
},
|
|
|
|
| 33 |
...options,
|
| 34 |
});
|
| 35 |
|
| 36 |
if (!response.ok) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
// Handle 429 (Too Many Requests) with exponential backoff
|
| 38 |
if (response.status === 429 && retryCount < 3) {
|
| 39 |
const backoffDelay = Math.pow(2, retryCount) * 1000; // 1s, 2s, 4s
|
|
|
|
| 30 |
"Content-Type": "application/json",
|
| 31 |
...options?.headers,
|
| 32 |
},
|
| 33 |
+
credentials: "include", // Ensure cookies are sent with requests
|
| 34 |
...options,
|
| 35 |
});
|
| 36 |
|
| 37 |
if (!response.ok) {
|
| 38 |
+
// Handle 401 (Unauthorized) - session expired, redirect to login
|
| 39 |
+
if (response.status === 401) {
|
| 40 |
+
console.warn("🔐 Session expired - redirecting to login...");
|
| 41 |
+
// Redirect to login page
|
| 42 |
+
window.location.href = "/auth/login-page";
|
| 43 |
+
throw new ApiError(401, "Session expired. Please log in again.");
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
// Handle 429 (Too Many Requests) with exponential backoff
|
| 47 |
if (response.status === 429 && retryCount < 3) {
|
| 48 |
const backoffDelay = Math.pow(2, retryCount) * 1000; // 1s, 2s, 4s
|