From 1279f46b1f442db5502f0e3b551fe2fed3b3fa32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20Tonh=C3=A4user?= Date: Tue, 11 Aug 2026 17:13:44 +0200 Subject: [PATCH] fix: redirect expired portal sessions --- src/app/admin/layout.tsx | 5 ++-- src/app/redaktion/layout.tsx | 5 ++-- src/modules/auth/page-access.test.ts | 42 ++++++++++++++++++++++++++++ src/modules/auth/page-access.ts | 21 ++++++++++++++ 4 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 src/modules/auth/page-access.test.ts create mode 100644 src/modules/auth/page-access.ts diff --git a/src/app/admin/layout.tsx b/src/app/admin/layout.tsx index e9e4634..a633995 100644 --- a/src/app/admin/layout.tsx +++ b/src/app/admin/layout.tsx @@ -3,10 +3,11 @@ import type { ReactNode } from "react"; import { headers } from "next/headers"; import { PortalShell } from "@/components/layout/portal-shell"; +import { requirePageAccess } from "@/modules/auth/page-access"; import { requireAdmin } from "@/modules/auth/principal"; export default async function AdminLayout({ children }: Readonly<{ children: ReactNode }>) { - await requireAdmin(new Headers(await headers())); + await requirePageAccess(async () => requireAdmin(new Headers(await headers()))); return ( ); -} \ No newline at end of file +} diff --git a/src/app/redaktion/layout.tsx b/src/app/redaktion/layout.tsx index b26c3bd..8d5b935 100644 --- a/src/app/redaktion/layout.tsx +++ b/src/app/redaktion/layout.tsx @@ -1,11 +1,12 @@ import type { ReactNode } from "react"; import { PortalShell } from "@/components/layout/portal-shell"; +import { requirePageAccess } from "@/modules/auth/page-access"; import { requireEditorial } from "./access"; export default async function EditorialLayout({ children }: Readonly<{ children: ReactNode }>) { - await requireEditorial(); + await requirePageAccess(requireEditorial); return ( ); -} \ No newline at end of file +} diff --git a/src/modules/auth/page-access.test.ts b/src/modules/auth/page-access.test.ts new file mode 100644 index 0000000..bf0c2ee --- /dev/null +++ b/src/modules/auth/page-access.test.ts @@ -0,0 +1,42 @@ +import { afterEach, expect, test, vi } from "vitest"; + +const { redirect } = vi.hoisted(() => ({ + redirect: vi.fn((destination: string): never => { + throw new Error(`NEXT_REDIRECT:${destination}`); + }), +})); + +vi.mock("next/navigation", () => ({ redirect })); + +import { PrincipalAuthorizationError } from "./principal"; +import { requirePageAccess } from "./page-access"; + +afterEach(() => vi.clearAllMocks()); + +test("redirects an expired protected-page session to sign-in", async () => { + const expired = new PrincipalAuthorizationError("AUTHENTICATION_REQUIRED"); + + await expect( + requirePageAccess(async () => { + throw expired; + }), + ).rejects.toThrow("NEXT_REDIRECT:/anmelden"); +}); + +test("rethrows non-session failures unchanged", async () => { + const unavailable = new Error("database unavailable"); + + await expect( + requirePageAccess(async () => { + throw unavailable; + }), + ).rejects.toBe(unavailable); + expect(redirect).not.toHaveBeenCalled(); +}); + +test("returns the principal when the page guard succeeds", async () => { + const principal = { userId: "admin-1" }; + + await expect(requirePageAccess(async () => principal)).resolves.toBe(principal); + expect(redirect).not.toHaveBeenCalled(); +}); diff --git a/src/modules/auth/page-access.ts b/src/modules/auth/page-access.ts new file mode 100644 index 0000000..a72fdbd --- /dev/null +++ b/src/modules/auth/page-access.ts @@ -0,0 +1,21 @@ +import "server-only"; + +import { redirect } from "next/navigation"; + +import { PrincipalAuthorizationError } from "@/modules/auth/principal"; + +export async function requirePageAccess( + loadPrincipal: () => Promise, +): Promise { + try { + return await loadPrincipal(); + } catch (error) { + if ( + error instanceof PrincipalAuthorizationError && + error.code === "AUTHENTICATION_REQUIRED" + ) { + redirect("/anmelden"); + } + throw error; + } +}