fix: prevent redirect loop on /account/setup for users without profile
- Skip profile redirect when already on /account/setup so users who just signed up/logged in land on the page without looping - Add test for the skip-redirect case
This commit is contained in:
@@ -2,7 +2,7 @@ import type { LayoutServerLoad } from './$types';
|
||||
import { error, redirect } from '@sveltejs/kit';
|
||||
import { prisma } from '$lib/db';
|
||||
|
||||
export const load: LayoutServerLoad = async ({ locals }) => {
|
||||
export const load: LayoutServerLoad = async ({ locals, url }) => {
|
||||
if (!locals.user?.id) {
|
||||
throw error(403, 'not authorized');
|
||||
}
|
||||
@@ -14,7 +14,8 @@ export const load: LayoutServerLoad = async ({ locals }) => {
|
||||
if (!user) {
|
||||
throw error(403, 'not authorized');
|
||||
}
|
||||
if (!user.profile) {
|
||||
if (!user.profile && !url.pathname.startsWith('/account/setup')) {
|
||||
// Skip redirect when already on /account/setup to avoid redirect loop
|
||||
throw redirect(303, '/account/setup');
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,8 @@ describe('(protected) layout server load', () => {
|
||||
findUnique.mockResolvedValue({ id: 'user-1', roleId: null, profile: null });
|
||||
|
||||
const locals = createLocals(mockUser() as any);
|
||||
const err = await Promise.resolve(load({ locals } as any)).catch((e: unknown) => e);
|
||||
const url = new URL('http://localhost/dashboard');
|
||||
const err = await Promise.resolve(load({ locals, url } as any)).catch((e: unknown) => e);
|
||||
expect(isRedirect(err)).toBe(true);
|
||||
if (isRedirect(err)) {
|
||||
expect(err.status).toBe(303);
|
||||
@@ -49,6 +50,15 @@ describe('(protected) layout server load', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('skips redirect when already on /account/setup', async () => {
|
||||
findUnique.mockResolvedValue({ id: 'user-1', roleId: null, profile: null });
|
||||
|
||||
const locals = createLocals(mockUser() as any);
|
||||
const url = new URL('http://localhost/account/setup');
|
||||
const result = await load({ locals, url } as any);
|
||||
expect(result).toEqual({ userRoleId: null });
|
||||
});
|
||||
|
||||
it('returns userRoleId when user has a profile', async () => {
|
||||
findUnique.mockResolvedValue(mockUserWithProfile({ roleId: 2 }));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user