From 8fcb83a9523b09763cf64b912b467aed5f43d219 Mon Sep 17 00:00:00 2001 From: Daniel Xu Date: Fri, 29 May 2026 12:46:18 -0700 Subject: [PATCH] Fix sign-in redirect stall --- src/lib/auth-redirect.test.ts | 5 ++++- src/lib/auth-redirect.ts | 16 +++++++++++----- src/lib/components/post-auth-redirect.svelte | 10 ++++++++++ src/lib/server/auth.ts | 2 +- src/routes/(marketing)/sign-in/+page.svelte | 14 +++++++++++--- 5 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 src/lib/components/post-auth-redirect.svelte diff --git a/src/lib/auth-redirect.test.ts b/src/lib/auth-redirect.test.ts index 710e032..5de1b18 100644 --- a/src/lib/auth-redirect.test.ts +++ b/src/lib/auth-redirect.test.ts @@ -16,11 +16,14 @@ describe('safeRedirectPath', () => { }); describe('readPostAuthRedirectUrl', () => { - it('prefers redirect_url and supports redirectUrl', () => { + it('prefers redirect_url and supports Clerk redirect params', () => { expect(readPostAuthRedirectUrl(new URLSearchParams('redirect_url=/runs'))).toBe('/runs'); expect(readPostAuthRedirectUrl(new URLSearchParams('redirectUrl=/regression'))).toBe( '/regression' ); + expect(readPostAuthRedirectUrl(new URLSearchParams('redirectAfterAuth=/runs/123'))).toBe( + '/runs/123' + ); }); it('falls back when params are missing or unsafe', () => { diff --git a/src/lib/auth-redirect.ts b/src/lib/auth-redirect.ts index aa3233c..2d2ed6c 100644 --- a/src/lib/auth-redirect.ts +++ b/src/lib/auth-redirect.ts @@ -1,6 +1,9 @@ -const defaultPostAuthPath = '/runs'; +const defaultPostAuthPath: `/${string}` = '/runs'; -export function safeRedirectPath(path: string | null | undefined, fallback = defaultPostAuthPath) { +export function safeRedirectPath( + path: string | null | undefined, + fallback: `/${string}` = defaultPostAuthPath +): `/${string}` { if ( !path || !path.startsWith('/') || @@ -10,13 +13,16 @@ export function safeRedirectPath(path: string | null | undefined, fallback = def ) { return fallback; } - return path; + return path as `/${string}`; } export function readPostAuthRedirectUrl( searchParams: URLSearchParams, - fallback = defaultPostAuthPath + fallback: `/${string}` = defaultPostAuthPath ) { - const raw = searchParams.get('redirect_url') ?? searchParams.get('redirectUrl'); + const raw = + searchParams.get('redirect_url') ?? + searchParams.get('redirectUrl') ?? + searchParams.get('redirectAfterAuth'); return safeRedirectPath(raw, fallback); } diff --git a/src/lib/components/post-auth-redirect.svelte b/src/lib/components/post-auth-redirect.svelte new file mode 100644 index 0000000..1ca5cb2 --- /dev/null +++ b/src/lib/components/post-auth-redirect.svelte @@ -0,0 +1,10 @@ + diff --git a/src/lib/server/auth.ts b/src/lib/server/auth.ts index 30f7d27..991d3a0 100644 --- a/src/lib/server/auth.ts +++ b/src/lib/server/auth.ts @@ -5,7 +5,7 @@ import { error, redirect, type RequestEvent } from '@sveltejs/kit'; export { safeRedirectPath } from '$lib/auth-redirect'; -export function readPostAuthRedirectUrl(event: RequestEvent, fallback?: string) { +export function readPostAuthRedirectUrl(event: RequestEvent, fallback?: `/${string}`) { return readPostAuthRedirectUrlFromSearchParams(event.url.searchParams, fallback); } diff --git a/src/routes/(marketing)/sign-in/+page.svelte b/src/routes/(marketing)/sign-in/+page.svelte index 2b9d300..0d3e6b4 100644 --- a/src/routes/(marketing)/sign-in/+page.svelte +++ b/src/routes/(marketing)/sign-in/+page.svelte @@ -1,6 +1,7 @@ @@ -26,8 +29,13 @@ - - + + {#if clerk?.user} + + + {:else} + + {/if}