fix: redirect expired portal sessions
This commit is contained in:
1 parent
b1ef4a8186
commit
1279f46b1f
4 files changed
+69
-4
No files matched your search
@@ -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 (
|
||||
<PortalShell
|
||||
@@ -23,4 +24,4 @@ export default async function AdminLayout({ children }: Readonly<{ children: Rea
|
||||
{children}
|
||||
</PortalShell>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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 (
|
||||
<PortalShell
|
||||
@@ -21,4 +22,4 @@ export default async function EditorialLayout({ children }: Readonly<{ children:
|
||||
{children}
|
||||
</PortalShell>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
import "server-only";
|
||||
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
import { PrincipalAuthorizationError } from "@/modules/auth/principal";
|
||||
|
||||
export async function requirePageAccess<T>(
|
||||
loadPrincipal: () => Promise<T>,
|
||||
): Promise<T> {
|
||||
try {
|
||||
return await loadPrincipal();
|
||||
} catch (error) {
|
||||
if (
|
||||
error instanceof PrincipalAuthorizationError &&
|
||||
error.code === "AUTHENTICATION_REQUIRED"
|
||||
) {
|
||||
redirect("/anmelden");
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user