From 55a6f1e437197aa17302a2261201a09d5fbd5f59 Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Wed, 25 Nov 2026 10:05:58 -0500 Subject: [PATCH] fix(web): opening an admin or Collection page directly no longer bounces an admin to Home The two route gates read the signed-in user without waiting for the root layout to load it. SvelteKit runs layout loads side by side, so a refresh or a bookmark found no user yet and redirected to Home. Each gate now awaits parent() before it judges. Co-Authored-By: Claude Opus 5.5 --- web/src/routes/admin/+layout.ts | 9 ++++++--- web/src/routes/admin/admin.test.ts | 23 ++++++++++++++++------- web/src/routes/collection/+layout.ts | 10 ++++++---- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/web/src/routes/admin/+layout.ts b/web/src/routes/admin/+layout.ts index 2f10dd9f..01094888 100644 --- a/web/src/routes/admin/+layout.ts +++ b/web/src/routes/admin/+layout.ts @@ -3,9 +3,12 @@ import { user } from '#lib/auth/store.svelte.js'; import type { LayoutLoad } from './$types'; // Hard route gate: runs before the layout (and any child page) renders. -// Auth is guaranteed bootstrapped by the root +layout.ts, so user.value is -// either a settled User object or null. -export const load: LayoutLoad = () => { +// SvelteKit runs layout loads side by side, so this waits for the root +// +layout.ts to finish signing the user in; otherwise opening an admin page +// directly (a refresh, a bookmark) would find no user yet and bounce an +// admin to Home. +export const load: LayoutLoad = async ({ parent }) => { + await parent(); if (!user.value || !user.value.is_admin) { throw redirect(302, '/'); } diff --git a/web/src/routes/admin/admin.test.ts b/web/src/routes/admin/admin.test.ts index 973c57f8..509285e4 100644 --- a/web/src/routes/admin/admin.test.ts +++ b/web/src/routes/admin/admin.test.ts @@ -38,26 +38,35 @@ afterEach(() => { }); describe('admin/+layout.ts load gate', () => { + // The root layout signs the user in; parent() resolves once it has. + const signedIn = { parent: async () => ({}) } as unknown as Parameters[0]; + test('non-admin user throws redirect to /', async () => { userState.current = { id: 'u1', username: 'alice', is_admin: false }; const { load } = await import('./+layout'); - await expect(async () => - load({} as Parameters[0]) - ).rejects.toMatchObject({ status: 302, location: '/' }); + await expect(load(signedIn)).rejects.toMatchObject({ status: 302, location: '/' }); }); test('unauthenticated (null user) throws redirect to /', async () => { userState.current = null; const { load } = await import('./+layout'); - await expect(async () => - load({} as Parameters[0]) - ).rejects.toMatchObject({ status: 302, location: '/' }); + await expect(load(signedIn)).rejects.toMatchObject({ status: 302, location: '/' }); }); test('admin user passes the gate', async () => { userState.current = { id: 'u1', username: 'root', is_admin: true }; const { load } = await import('./+layout'); - expect(load({} as Parameters[0])).toEqual({}); + await expect(load(signedIn)).resolves.toEqual({}); + }); + + test('opening an admin page directly waits for sign-in instead of bouncing an admin', async () => { + userState.current = null; + const { load } = await import('./+layout'); + const parent = async () => { + userState.current = { id: 'u1', username: 'root', is_admin: true }; + return {}; + }; + await expect(load({ parent } as unknown as Parameters[0])).resolves.toEqual({}); }); }); diff --git a/web/src/routes/collection/+layout.ts b/web/src/routes/collection/+layout.ts index 0a1880c3..aa1773e8 100644 --- a/web/src/routes/collection/+layout.ts +++ b/web/src/routes/collection/+layout.ts @@ -2,10 +2,12 @@ import { redirect } from '@sveltejs/kit'; import { user } from '#lib/auth/store.svelte.js'; import type { LayoutLoad } from './$types'; -// Hard route gate: the Collection is an admin's. Runs before the -// layout renders; auth is bootstrapped by the root +layout.ts, so user.value is -// either a settled User object or null. -export const load: LayoutLoad = () => { +// Hard route gate: the Collection is an admin's. SvelteKit runs layout loads +// side by side, so this waits for the root +layout.ts to finish signing the +// user in; otherwise opening a Collection page directly (a refresh, a +// bookmark) would find no user yet and bounce an admin to Home. +export const load: LayoutLoad = async ({ parent }) => { + await parent(); if (!user.value || !user.value.is_admin) { throw redirect(302, '/'); }