From 44bfb092452332083f958700990575b40ea39c42 Mon Sep 17 00:00:00 2001 From: Alexander Bocken Date: Sun, 31 Aug 2025 22:04:27 +0200 Subject: [PATCH] Implement proper page redirects for protected routes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Update hooks.server.ts to preserve original URL when redirecting to login - Use callbackUrl parameter to maintain user's intended destination - Preserve both pathname and search parameters in redirect flow - Leverage OIDC standard callback URL support built into Auth.js - Users now land exactly where they intended after authentication - Works for /rezepte/add, /rezepte/edit/[name], and any future protected routes 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/hooks.server.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/hooks.server.ts b/src/hooks.server.ts index c189a46..0aedab4 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -12,7 +12,9 @@ async function authorization({ event, resolve }) { if (event.url.pathname.startsWith('/rezepte/edit') || event.url.pathname.startsWith('/rezepte/add')) { const session = await event.locals.auth(); if (!session) { - redirect(303, '/auth/signin'); + // Preserve the original URL the user was trying to access + const callbackUrl = encodeURIComponent(event.url.pathname + event.url.search); + redirect(303, `/login?callbackUrl=${callbackUrl}`); } else if (! session.user.groups.includes('rezepte_users')) { // strip last dir from url