Skip to content
This repository was archived by the owner on Aug 2, 2025. It is now read-only.

Commit 51180a1

Browse files
committed
fix: misc auth issues
1 parent b18152c commit 51180a1

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

api/token.mjs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import fetch from "node-fetch";
22
import cookie from "cookie";
33

44
export default async function handler(request, response) {
5+
if (process.env.VERCEL_ENV === "development") {
6+
console.log("Running in development mode...");
7+
}
8+
59
switch (request.method) {
610
case "POST":
711
try {
@@ -102,17 +106,18 @@ export default async function handler(request, response) {
102106
let data;
103107

104108
if (!res.ok) {
105-
response.status(500).send("Bad response from server.");
106-
return;
107-
}
108-
109-
try {
110-
data = await res.json();
111-
} catch {
112-
if (data && data?.["error"] == "invalid_grant") {
113-
response.status(500).send("INVALID GRANT");
109+
try {
110+
const json = await res.json();
111+
if (json["error"] == "invalid_grant") {
112+
response.status(400).send("invalid_grant");
113+
return;
114+
}
115+
} catch {
116+
response.status(500).send("Bad json from server.");
114117
return;
115118
}
119+
response.status(500).send("Bad response from server.");
120+
return;
116121
}
117122

118123
response.setHeader("Set-Cookie", [
@@ -132,7 +137,7 @@ export default async function handler(request, response) {
132137
}),
133138
]);
134139

135-
response.status(200).send("");
140+
response.status(200).send("Sucessfully refreshed token");
136141
break;
137142
}
138143
}

src/Auth.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,20 @@ export class Auth {
121121
},
122122
});
123123

124+
try {
125+
const error = await res.text();
126+
127+
if (error === "invalid_grant") {
128+
console.log("invalid_grant");
129+
this.shouldLogin = true;
130+
this.refreshing = false;
131+
rerender?.();
132+
return;
133+
}
134+
} catch {
135+
throw new Error("Can't read text from response");
136+
}
137+
124138
if (!res.ok) {
125139
throw new HTTPError(res.status);
126140
}

src/services/sbhsApi/fetchSbhsApi.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,10 @@ export const fetchSbhsApi = async <TSBHSAPIData>(
5656
new CustomEvent("onlinechange", { detail: { online: false } })
5757
);
5858
throw new NetworkError("SBHS API server is not available");
59-
default:
60-
if (res.status === 401) {
61-
auth.shouldLogin = true;
62-
throw new UnauthorizedError();
63-
}
59+
}
60+
if (res.status === 401) {
61+
auth.shouldLogin = true;
62+
throw new UnauthorizedError();
6463
}
6564
throw new HTTPError(res.status);
6665
}

vite.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export default defineConfig({
2323
],
2424
mode: "production", // this inlines the module imports when using yarn build
2525
}),
26+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
27+
// @ts-ignore
2628
replace({
2729
is_vite_preview: true, // this is used to conditionally call Workbox's precacheAndRoute function
2830
preventAssignment: true,

0 commit comments

Comments
 (0)