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

Commit 8608b45

Browse files
committed
fix: make access token work
1 parent c4c1ded commit 8608b45

File tree

2 files changed

+44
-59
lines changed

2 files changed

+44
-59
lines changed

api/token.mjs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,12 @@ export default async function handler(request, response) {
106106
let data;
107107

108108
if (!res.ok) {
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.");
117-
return;
118-
}
119109
response.status(500).send("Bad response from server.");
120110
return;
121111
}
122112

113+
data = await res.json();
114+
123115
response.setHeader("Set-Cookie", [
124116
cookie.serialize("access_token", data["access_token"], {
125117
httpOnly: true,

src/stores/auth/sbhsAuth.ts

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -260,49 +260,6 @@ export const sbhsAuthActions = {
260260
refresh: async () => {
261261
// Check if valid state to refresh
262262
switch (useSbhsAuthStore.getState().status) {
263-
case SbhsAuthStatus.LOGGED_IN:
264-
// Set status to refreshing
265-
useSbhsAuthStore.setState(
266-
produce(useSbhsAuthStore.getState(), (draft) => {
267-
draft.status = SbhsAuthStatus.REFRESHING;
268-
})
269-
);
270-
try {
271-
const res = await fetch("/api/token", {
272-
method: "PATCH",
273-
body: JSON.stringify({
274-
client_id: config.client_id,
275-
}),
276-
headers: {
277-
"Content-Type": "application/json; charset=UTF-8",
278-
},
279-
});
280-
281-
if (!res.ok) {
282-
throw new HTTPError(res.status);
283-
}
284-
285-
useSbhsAuthStore.setState(
286-
produce(useSbhsAuthStore.getState(), (draft) => {
287-
draft.status = SbhsAuthStatus.LOGGED_IN;
288-
})
289-
);
290-
} catch (error) {
291-
useSbhsAuthStore.setState(
292-
produce(useSbhsAuthStore.getState(), (draft) => {
293-
draft.status = SbhsAuthStatus.EXPIRED;
294-
})
295-
);
296-
toast({
297-
title:
298-
"Something went wrong, try logging in and out if the issue persists.",
299-
description: error.message,
300-
status: "error",
301-
isClosable: true,
302-
});
303-
throw error;
304-
}
305-
break;
306263
// If we are already refreshing, wait for the refresh to finish
307264
case SbhsAuthStatus.REFRESHING: {
308265
const promise = new Promise<void>((resolve, reject) => {
@@ -317,13 +274,49 @@ export const sbhsAuthActions = {
317274
});
318275
return promise;
319276
}
320-
// Warn if invalid usage occurs
321277
default:
322-
console.warn(
323-
`Invalid usage of refresh action with auth status ${
324-
useSbhsAuthStore.getState().status
325-
}.`
326-
);
278+
if (logged_in_states.includes(useSbhsAuthStore.getState().status)) {
279+
// Set status to refreshing
280+
useSbhsAuthStore.setState(
281+
produce(useSbhsAuthStore.getState(), (draft) => {
282+
draft.status = SbhsAuthStatus.REFRESHING;
283+
})
284+
);
285+
try {
286+
const res = await fetch("/api/token", {
287+
method: "PATCH",
288+
body: JSON.stringify({
289+
client_id: config.client_id,
290+
}),
291+
headers: {
292+
"Content-Type": "application/json; charset=UTF-8",
293+
},
294+
});
295+
296+
if (!res.ok) {
297+
throw new HTTPError(res.status);
298+
}
299+
300+
useSbhsAuthStore.setState(
301+
produce(useSbhsAuthStore.getState(), (draft) => {
302+
draft.status = SbhsAuthStatus.LOGGED_IN;
303+
})
304+
);
305+
} catch (error) {
306+
useSbhsAuthStore.setState(
307+
produce(useSbhsAuthStore.getState(), (draft) => {
308+
draft.status = SbhsAuthStatus.EXPIRED;
309+
})
310+
);
311+
}
312+
} else {
313+
// Warn if invalid usage occurs
314+
console.warn(
315+
`Invalid usage of refresh action with auth status ${
316+
useSbhsAuthStore.getState().status
317+
}.`
318+
);
319+
}
327320
}
328321
},
329322
fetchAuthenticated: async <TSbhsApiData>(

0 commit comments

Comments
 (0)