Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(web): centralize sign-in redirects

Genera07bf8a38a06
diff --git a/src/web/src/lib/api.test.ts b/src/web/src/lib/api.test.ts
index 341f2e702..4aeb86e88 100644
--- a/src/web/src/lib/api.test.ts
+++ b/src/web/src/lib/api.test.ts
@@ -10,21 +10,22 @@ Object.defineProperty(globalThis, "document", {
// We need to mock fetch globally
const mockFetch = vi.fn();
vi.stubGlobal("fetch", mockFetch);
+const locationAssignMock = vi.fn();
// Prevent 401 redirect from blowing up
Object.defineProperty(globalThis, "window", {
value: {
...(globalThis.window || {}),
- location: { href: "" },
+ location: {
+ origin: "https://alook.test",
+ assign: locationAssignMock,
+ },
},
writable: true,
});
beforeEach(() => {
vi.clearAllMocks();
- if (typeof window !== "undefined") {
- window.location.href = "";
- }
});
describe("ApiError class", () => {
@@ -215,6 +216,7 @@ describe("apiFetch", () => {
expect((e as ApiError).status).toBe(401);
expect((e as ApiError).message).toBe("Unauthorized");
expect((e as ApiError).isUnauthorized).toBe(true);
+ expect(locationAssignMock).toHaveBeenCalledWith(new URL("/sign-in", "https://alook.test"));
}
});
});
@@ -338,4 +340,3 @@ describe("apiFetch — mock network delay", () => {
expect(result).toEqual([{ id: "a1" }]);
});
});
-
diff --git a/src/web/src/lib/api/client.ts b/src/web/src/lib/api/client.ts
index a6995e771..d455ab935 100644
--- a/src/web/src/lib/api/client.ts
+++ b/src/web/src/lib/api/client.ts
@@ -34,6 +34,19 @@ function getReadableErrorMessage(error: string | undefined, details: string[] |
return error;
}
+/**
+ * Leave the authenticated app with a full document navigation.
+ *
+ * API helpers do not have access to the Next router, and a 401 must discard
+ * all in-memory authenticated state rather than preserve the current React
+ * tree. Use an explicit same-origin absolute URL so this intentional hard
+ * navigation is not mistaken for an internal client-side route transition.
+ */
+export function redirectToSignIn() {
+ if (typeof window === "undefined") return;
+ window.location.assign(new URL("/sign-in", window.location.origin));
+}
+
export async function apiFetch<T>(path: string, options?: RequestInit): Promise<T> {
if (MOCK_NETWORK_ENABLED) {
if (!mockNetworkLogged) {
@@ -61,9 +74,7 @@ export async function apiFetch<T>(path: string, options?: RequestInit): Promise<
}
if (res.status === 401) {
- if (typeof window !== "undefined") {
- window.location.href = "/sign-in";
- }
+ redirectToSignIn();
throw new ApiError("Unauthorized", 401);
}
diff --git a/src/web/src/lib/api/conversations.ts b/src/web/src/lib/api/conversations.ts
index cf852e51f..1dc78a27f 100644
--- a/src/web/src/lib/api/conversations.ts
+++ b/src/web/src/lib/api/conversations.ts
@@ -7,7 +7,7 @@ import type {
} from "@alook/shared";
import { ApiError } from "@/lib/errors";
import type { PendingFile } from "@/hooks/use-file-attachments";
-import { apiFetch, wsQuery } from "./client";
+import { apiFetch, redirectToSignIn, wsQuery } from "./client";
export const listConversations = (workspaceId: string, channel?: string) =>
apiFetch<Conversation[]>(`/api/conversations${wsQuery(workspaceId, channel ? { channel } : undefined)}`);
@@ -180,7 +180,7 @@ export const sendMessage = async (
}
if (res.status === 401) {
- if (typeof window !== "undefined") window.location.href = "/sign-in";
+ redirectToSignIn();
throw new ApiError("Unauthorized", 401);
}
diff --git a/src/web/src/lib/api/issues.ts b/src/web/src/lib/api/issues.ts
index 53ec49488..d8ed75f2b 100644
--- a/src/web/src/lib/api/issues.ts
+++ b/src/web/src/lib/api/issues.ts
@@ -8,7 +8,7 @@ import type {
UpdateIssueRequest,
} from "@alook/shared";
import { ApiError } from "@/lib/errors";
-import { apiFetch, wsQuery } from "./client";
+import { apiFetch, redirectToSignIn, wsQuery } from "./client";
export type IssueListItem = Issue & { thread_agent_ids?: string[] };
@@ -68,7 +68,7 @@ export const createIssue = async (
}
if (res.status === 401) {
- if (typeof window !== "undefined") window.location.href = "/sign-in";
+ redirectToSignIn();
throw new ApiError("Unauthorized", 401);
}
diff --git a/src/web/src/lib/api/workspaces.ts b/src/web/src/lib/api/workspaces.ts
index 6ac002553..f77fae49d 100644
--- a/src/web/src/lib/api/workspaces.ts
+++ b/src/web/src/lib/api/workspaces.ts
@@ -1,6 +1,6 @@
import type { LoginResponse, Workspace } from "@alook/shared";
import { sanitizeSlug } from "@alook/shared";
-import { apiFetch, wsQuery } from "./client";
+import { apiFetch, redirectToSignIn, wsQuery } from "./client";
export const listWorkspaces = () => apiFetch<Workspace[]>("/api/workspaces");
@@ -120,9 +120,7 @@ export const getWorkspaceOverview = (workspaceId: string) =>
// Auth
export const signOut = async () => {
- if (typeof window !== "undefined") {
- window.location.href = "/sign-in";
- }
+ redirectToSignIn();
};
export const verifyCode = (email: string, code: string) =>