feat(auth): preserve redirect URL when redirecting to /login

Appends ?redirect= with the original pathname so the login page
can redirect back after successful authentication.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-02 13:56:49 +02:00
parent cc74c0042a
commit 92c7d8f92e
2 changed files with 12 additions and 8 deletions

View File

@@ -57,15 +57,14 @@ describe('auth guard (hooks.server.ts handle)', () => {
}
);
it('redirects unauthenticated requests on protected routes', async () => {
const { event, resolve } = createEvent('/planner');
it('redirects unauthenticated requests to /login with redirect param', async () => {
const { event, resolve } = createEvent('/recipes/abc');
try {
await handle({ event, resolve });
// If using SvelteKit redirect, it throws
expect.unreachable();
} catch (e: any) {
expect(e.status).toBe(302);
expect(e.location).toBe('/login');
expect(e.location).toBe('/login?redirect=%2Frecipes%2Fabc');
}
});
@@ -99,7 +98,7 @@ describe('auth guard (hooks.server.ts handle)', () => {
expect(resolve).toHaveBeenCalledWith(event);
});
it('redirects to /login when session validation fails', async () => {
it('redirects to /login with redirect param when session validation fails', async () => {
mockGet.mockResolvedValue({ data: undefined, error: { status: 401 } });
const { event, resolve } = createEvent('/planner', 'bad-session');
@@ -108,7 +107,7 @@ describe('auth guard (hooks.server.ts handle)', () => {
expect.unreachable();
} catch (e: any) {
expect(e.status).toBe(302);
expect(e.location).toBe('/login');
expect(e.location).toBe('/login?redirect=%2Fplanner');
}
});
});