Spaces:
Running
Running
dylanebert
commited on
Commit
·
5261a1c
1
Parent(s):
7ceb966
Fix OAuth token handling in frontend
Browse files- Properly handle null/undefined tokens in fetchScenes API
- Don't send Authorization header when no token exists
- Prevents sending 'null' string as access token to backend
- Fixes 500 errors when users haven't authenticated yet
src/routes/Vote.svelte
CHANGED
@@ -45,12 +45,18 @@
|
|
45 |
try {
|
46 |
const url = "/api/fetchScenes";
|
47 |
const token = localStorage.getItem("access_token");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
const response = await fetch(url, {
|
49 |
method: "GET",
|
50 |
-
headers
|
51 |
-
"Cache-Control": "no-cache",
|
52 |
-
Authorization: `Bearer ${token}`,
|
53 |
-
},
|
54 |
});
|
55 |
const result = await response.json();
|
56 |
if (result.input) {
|
|
|
45 |
try {
|
46 |
const url = "/api/fetchScenes";
|
47 |
const token = localStorage.getItem("access_token");
|
48 |
+
const headers = {
|
49 |
+
"Cache-Control": "no-cache",
|
50 |
+
};
|
51 |
+
|
52 |
+
// Only add Authorization header if we have a valid token
|
53 |
+
if (token) {
|
54 |
+
headers.Authorization = `Bearer ${token}`;
|
55 |
+
}
|
56 |
+
|
57 |
const response = await fetch(url, {
|
58 |
method: "GET",
|
59 |
+
headers,
|
|
|
|
|
|
|
60 |
});
|
61 |
const result = await response.json();
|
62 |
if (result.input) {
|
src/routes/api/fetchScenes/+server.ts
CHANGED
@@ -5,7 +5,11 @@ export const GET: RequestHandler = async ({ request }) => {
|
|
5 |
const authHeader = request.headers.get("authorization");
|
6 |
let accessToken = null;
|
7 |
if (authHeader && authHeader.startsWith("Bearer ")) {
|
8 |
-
|
|
|
|
|
|
|
|
|
9 |
}
|
10 |
|
11 |
const url = accessToken
|
|
|
5 |
const authHeader = request.headers.get("authorization");
|
6 |
let accessToken = null;
|
7 |
if (authHeader && authHeader.startsWith("Bearer ")) {
|
8 |
+
const token = authHeader.substring("Bearer ".length);
|
9 |
+
// Don't use token if it's null, undefined, or empty string
|
10 |
+
if (token && token !== "null" && token !== "undefined") {
|
11 |
+
accessToken = token;
|
12 |
+
}
|
13 |
}
|
14 |
|
15 |
const url = accessToken
|