From 6be67e2329178855a51c909bdabeebd10eb69d0b Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 26 Jan 2026 16:23:34 +0100 Subject: [PATCH] Fix redirect loop after admin setup by invalidating setup cache The setupCheckHandle hook caches whether setup is needed for 1 minute. After creating the admin, this cache wasn't cleared, causing a redirect loop between /login and /setup. Co-Authored-By: Claude Opus 4.5 --- src/hooks.server.ts | 8 ++++++++ src/routes/setup/+page.server.ts | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/src/hooks.server.ts b/src/hooks.server.ts index ce2fd94..3726704 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -11,6 +11,14 @@ import { supabaseAdmin } from '$lib/server/supabase'; let setupCheckCache: { needsSetup: boolean; checkedAt: number } | null = null; const SETUP_CACHE_TTL = 60000; // 1 minute cache +/** + * Invalidate the setup check cache + * Call this after creating the first admin user + */ +export function invalidateSetupCache() { + setupCheckCache = null; +} + /** * Supabase authentication hook * Sets up the Supabase client with cookie handling for SSR diff --git a/src/routes/setup/+page.server.ts b/src/routes/setup/+page.server.ts index 981582b..4df8941 100644 --- a/src/routes/setup/+page.server.ts +++ b/src/routes/setup/+page.server.ts @@ -2,6 +2,7 @@ import { fail, redirect } from '@sveltejs/kit'; import type { Actions, PageServerLoad } from './$types'; import { supabaseAdmin } from '$lib/server/supabase'; import { sendEmail, wrapInMonacoTemplate } from '$lib/server/email'; +import { invalidateSetupCache } from '../../hooks.server'; export const load: PageServerLoad = async () => { // Check if any users exist in the system @@ -249,6 +250,9 @@ export const actions: Actions = { // Non-critical - continue anyway since the account was created successfully } + // Clear the setup cache so other routes don't redirect back to /setup + invalidateSetupCache(); + // Success - redirect to login throw redirect(303, '/login?setup=complete'); }