feat(web): add logout button for web mode

This commit is contained in:
lxk955 authored and GitHub committed 2026-10-04 02:03:56 +08:00
1 parent 7aa69a0e5e
commit e3ea4740c0
19 files changed
+405 -3

No files matched your search

@@ -1,7 +1,7 @@
// @vitest-environment happy-dom
import { afterEach, describe, expect, it, vi } from "vitest";
import { checkStartupAuthentication } from "@/lib/startup/startupAuthentication";
import { checkStartupAuthentication, logoutWeb } from "@/lib/startup/startupAuthentication";
afterEach(() => vi.unstubAllGlobals());
@@ -31,4 +31,22 @@ describe("startup authentication", () => {
await expect(checkStartupAuthentication(controller.signal)).rejects.toThrow("AUTH_CHECK_FAILED");
expect(fetchMock.mock.calls[0]?.[1].signal).toBe(controller.signal);
});
describe("logoutWeb", () => {
it("calls POST /api/auth/logout with same-origin credentials", async () => {
const fetchMock = vi.fn().mockResolvedValue({ ok: true, json: async () => ({ ok: true }) });
vi.stubGlobal("fetch", fetchMock);
await logoutWeb();
expect(fetchMock).toHaveBeenCalledWith("/api/auth/logout", {
method: "POST",
credentials: "same-origin",
});
});
it("throws AUTH_LOGOUT_FAILED when server responds with error status", async () => {
const fetchMock = vi.fn().mockResolvedValue({ ok: false, status: 500 });
vi.stubGlobal("fetch", fetchMock);
await expect(logoutWeb()).rejects.toThrow("AUTH_LOGOUT_FAILED");
});
});
});
@@ -19,3 +19,13 @@ export async function checkStartupAuthentication(signal?: AbortSignal): Promise<
setup_required: "setup_required" in result && result.setup_required === true,
};
}
export async function logoutWeb(): Promise<void> {
const response = await fetch(apiUrl("/api/auth/logout"), {
method: "POST",
credentials: "same-origin",
});
if (!response.ok) {
throw new Error("AUTH_LOGOUT_FAILED");
}
}