dylanebert commited on
Commit
b9a606b
·
1 Parent(s): c612ee1

Fix authentication flow for invalid tokens

Browse files

- Improve token validation to catch 'null'/'undefined' strings
- Redirect to authorization for invalid tokens before voting
- Reset vote overlay state on errors to prevent UI freeze
- Add token validation in vote API endpoint

src/routes/Vote.svelte CHANGED
@@ -120,7 +120,7 @@
120
  voteOverlayB.classList.add("show");
121
 
122
  const token = localStorage.getItem("access_token");
123
- if (!token) {
124
  window.location.href = "/api/authorize";
125
  return;
126
  }
@@ -164,11 +164,17 @@
164
  window.location.href = "/api/authorize";
165
  } else {
166
  errorMessage = "Failed to process vote.";
 
 
 
167
  }
168
  }
169
  } catch (error) {
170
  errorMessage = "Failed to process vote.";
171
  statusMessage = "";
 
 
 
172
  }
173
  }
174
 
 
120
  voteOverlayB.classList.add("show");
121
 
122
  const token = localStorage.getItem("access_token");
123
+ if (!token || token === "null" || token === "undefined") {
124
  window.location.href = "/api/authorize";
125
  return;
126
  }
 
164
  window.location.href = "/api/authorize";
165
  } else {
166
  errorMessage = "Failed to process vote.";
167
+ voteOverlayA.classList.remove("show");
168
+ voteOverlayB.classList.remove("show");
169
+ voteOverlay = false;
170
  }
171
  }
172
  } catch (error) {
173
  errorMessage = "Failed to process vote.";
174
  statusMessage = "";
175
+ voteOverlayA.classList.remove("show");
176
+ voteOverlayB.classList.remove("show");
177
+ voteOverlay = false;
178
  }
179
  }
180
 
src/routes/api/vote/+server.ts CHANGED
@@ -8,6 +8,9 @@ export const POST: RequestHandler = async ({ request }) => {
8
  }
9
 
10
  const accessToken = authHeader.substring("Bearer ".length);
 
 
 
11
 
12
  const payload = await request.json();
13
  payload.access_token = accessToken;
 
8
  }
9
 
10
  const accessToken = authHeader.substring("Bearer ".length);
11
+ if (!accessToken || accessToken === "null" || accessToken === "undefined") {
12
+ return new Response(JSON.stringify({ error: "Unauthorized" }), { status: 401 });
13
+ }
14
 
15
  const payload = await request.json();
16
  payload.access_token = accessToken;