🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
fix(auth): stop white flash when redirecting to login (#1335)
simpleC0de159d9aef30e6
diff --git a/client/src/components/AuthenticationGuard.test.tsx b/client/src/components/AuthenticationGuard.test.tsxnew file mode 100644index 000000000..f6f2fb76d--- /dev/null+++ b/client/src/components/AuthenticationGuard.test.tsx@@ -0,0 +1,60 @@+import { cleanup, render } from "@testing-library/react";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++import { AuthenticationGuard } from "./AuthenticationGuard";++const mocks = vi.hoisted(() => ({+ pathname: "/",+ session: { user: null as { id: string } | null, isPending: false },+ redirect: vi.fn(),+ locationReplace: vi.fn(),+}));++vi.mock("next/navigation", () => ({+ usePathname: () => mocks.pathname,+ redirect: mocks.redirect,+}));++vi.mock("@/lib/userStore", () => ({ userStore: () => mocks.session }));++vi.mock("@/api/admin/hooks/useSites", () => ({+ useGetSiteIsPublic: () => ({ data: false, isLoading: false }),+}));++beforeEach(() => {+ mocks.pathname = "/";+ mocks.session = { user: null, isPending: false };+ vi.stubGlobal("location", { replace: mocks.locationReplace });+});+afterEach(() => {+ cleanup();+ vi.unstubAllGlobals();+ vi.clearAllMocks();+});++describe("AuthenticationGuard", () => {+ it("sends signed-out visitors to /login with a document navigation", () => {+ render(<AuthenticationGuard />);+ expect(mocks.locationReplace).toHaveBeenCalledWith("/login");+ // redirect() would unmount the root layout, <html> included, until /login has loaded.+ expect(mocks.redirect).not.toHaveBeenCalled();+ });++ it("waits for the session check before navigating", () => {+ mocks.session = { user: null, isPending: true };+ render(<AuthenticationGuard />);+ expect(mocks.locationReplace).not.toHaveBeenCalled();+ });++ it("leaves signed-in users where they are", () => {+ mocks.session = { user: { id: "user-1" }, isPending: false };+ render(<AuthenticationGuard />);+ expect(mocks.locationReplace).not.toHaveBeenCalled();+ });++ it("leaves public routes alone", () => {+ mocks.pathname = "/login";+ render(<AuthenticationGuard />);+ expect(mocks.locationReplace).not.toHaveBeenCalled();+ });+});diff --git a/client/src/components/AuthenticationGuard.tsx b/client/src/components/AuthenticationGuard.tsxindex aef90b86f..bcfd09c3b 100644--- a/client/src/components/AuthenticationGuard.tsx+++ b/client/src/components/AuthenticationGuard.tsx@@ -1,7 +1,7 @@"use client";import { useEffect } from "react";-import { redirect, usePathname } from "next/navigation";+import { usePathname } from "next/navigation";import { userStore } from "../lib/userStore";import { useGetSiteIsPublic } from "../api/admin/hooks/useSites";@@ -39,7 +39,10 @@ export function AuthenticationGuard() {!isPublicSite &&!hasPrivateKey) {- redirect("/login");+ // A document navigation keeps this page on screen until /login has painted. redirect() from an+ // effect unmounts <html> until then, dropping its theme class, and the URL-state sync's history+ // writes can discard a pending router.replace().+ window.location.replace("/login");}}, [isPending, user, pathname, isCheckingPublic, isPublicSite, hasPrivateKey]);