Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(auth): prevent 401 retry storm and redirect to sign-in

(justin)randoneeringfd444930b651
diff --git a/apps/web/src/fetchers/invitation/get-pending-invitations.ts b/apps/web/src/fetchers/invitation/get-pending-invitations.ts
index 25faf392d..e80b7c596 100644
--- a/apps/web/src/fetchers/invitation/get-pending-invitations.ts
+++ b/apps/web/src/fetchers/invitation/get-pending-invitations.ts
@@ -1,4 +1,5 @@
import { client } from "@kaneo/libs";
+import { HttpError } from "@/lib/http-error";
import type { WorkspaceUserInvitation } from "@/types/workspace-user";
export async function getPendingInvitations(): Promise<
@@ -7,7 +8,8 @@ export async function getPendingInvitations(): Promise<
const response = await client.invitation.pending.$get();
if (!response.ok) {
- throw new Error("Failed to get pending invitations");
+ const error = await response.text();
+ throw new HttpError(response.status, error || "Failed to get pending invitations");
}
return response.json();
diff --git a/apps/web/src/fetchers/task/get-tasks.ts b/apps/web/src/fetchers/task/get-tasks.ts
index b00eb5a02..b13cb1a1d 100644
--- a/apps/web/src/fetchers/task/get-tasks.ts
+++ b/apps/web/src/fetchers/task/get-tasks.ts
@@ -1,4 +1,5 @@
import { client } from "@kaneo/libs";
+import { HttpError } from "@/lib/http-error";
async function getTasks(projectId: string) {
const response = await client.task.tasks[":projectId"].$get({
@@ -9,7 +10,7 @@ async function getTasks(projectId: string) {
if (!response.ok) {
const error = await response.text();
- throw new Error(error);
+ throw new HttpError(response.status, error);
}
const json = await response.json();
diff --git a/apps/web/src/hooks/queries/task/use-get-tasks.ts b/apps/web/src/hooks/queries/task/use-get-tasks.ts
index e70c5e333..e4eb590a9 100644
--- a/apps/web/src/hooks/queries/task/use-get-tasks.ts
+++ b/apps/web/src/hooks/queries/task/use-get-tasks.ts
@@ -1,11 +1,13 @@
import { useQuery } from "@tanstack/react-query";
import getTasks from "@/fetchers/task/get-tasks";
+import { isUnauthorizedError } from "@/lib/http-error";
export function useGetTasks(projectId: string) {
return useQuery({
queryKey: ["tasks", projectId],
queryFn: () => getTasks(projectId),
- refetchInterval: 30000,
+ refetchInterval: (query) =>
+ isUnauthorizedError(query.state.error) ? false : 30000,
enabled: !!projectId,
});
}
diff --git a/apps/web/src/lib/http-error.ts b/apps/web/src/lib/http-error.ts
new file mode 100644
index 000000000..3e8c861de
--- /dev/null
+++ b/apps/web/src/lib/http-error.ts
@@ -0,0 +1,13 @@
+export class HttpError extends Error {
+ status: number;
+
+ constructor(status: number, message: string) {
+ super(message);
+ this.name = "HttpError";
+ this.status = status;
+ }
+}
+
+export function isUnauthorizedError(error: unknown): boolean {
+ return error instanceof HttpError && error.status === 401;
+}
diff --git a/apps/web/src/query-client/index.ts b/apps/web/src/query-client/index.ts
index 9178eb9c6..0c024399a 100644
--- a/apps/web/src/query-client/index.ts
+++ b/apps/web/src/query-client/index.ts
@@ -1,5 +1,6 @@
import * as Sentry from "@sentry/react";
import { MutationCache, QueryCache, QueryClient } from "@tanstack/react-query";
+import { isUnauthorizedError } from "@/lib/http-error";
// TanStack raises these before any fetcher-level message exists; CORS rejections
// from the browser also surface here. Used both to skip auto-retry and to tag
@@ -54,7 +55,13 @@ function captureCacheError(error: unknown, context: "query" | "mutation") {
const queryClient = new QueryClient({
queryCache: new QueryCache({
- onError: (error) => captureCacheError(error, "query"),
+ onError: (error) => {
+ if (isUnauthorizedError(error)) {
+ window.location.replace("/auth/sign-in");
+ return;
+ }
+ captureCacheError(error, "query");
+ },
}),
mutationCache: new MutationCache({
onError: (error) => captureCacheError(error, "mutation"),
@@ -64,7 +71,9 @@ const queryClient = new QueryClient({
refetchOnWindowFocus: false,
refetchOnMount: false,
retry: (failureCount, error) =>
- isNetworkError(error) ? false : failureCount < 2,
+ isNetworkError(error) || isUnauthorizedError(error)
+ ? false
+ : failureCount < 2,
},
mutations: {
retry: false,