Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/lib/graphql/graphqlFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,25 @@ export const graphqlFetch =
},
});
} catch (error) {
// Redirect to re-auth on 401 (expired/invalid token)
// Only sign out on a genuine 401 / UNAUTHENTICATED from the server.
// Network errors (aborted requests, timeouts during page transitions) are
// not ClientErrors and must never trigger sign-out; guarding `response`
// also avoids throwing a TypeError over the original error.
if (
error instanceof ClientError &&
error.response.status === 401 &&
typeof window !== "undefined"
typeof window !== "undefined" &&
error.response
) {
window.location.href = "/";
const isHttp401 = error.response.status === 401;
const isUnauthenticated = error.response.errors?.some(
(e) => e.extensions?.code === "UNAUTHENTICATED",
);

if (isHttp401 || isUnauthenticated) {
await fetch("/api/auth/sign-out", { method: "POST" });
window.location.href = "/";
return new Promise(() => {}) as Promise<TData>;
}
}

throw error;
Expand Down
Loading