diff --git a/__tests__/client/components.test.tsx b/__tests__/client/components.test.tsx index 4cbd85a..0a92c0b 100644 --- a/__tests__/client/components.test.tsx +++ b/__tests__/client/components.test.tsx @@ -100,6 +100,25 @@ describe('RequireAuth', () => { ); expect(loginWithRedirect).toHaveBeenCalledWith({ returnTo: '/dashboard' }); }); + + it('defaults returnTo to current pathname and search when not provided', () => { + const loginWithRedirect = vi.fn(); + const ctx = createMockAuth0Context({ + session: null, + isAuthenticated: false, + loginWithRedirect + }); + render( + + + x + + + ); + expect(loginWithRedirect).toHaveBeenCalledWith({ + returnTo: window.location.pathname + window.location.search + }); + }); }); // ─── RequireRole ────────────────────────────────────────────────────────────── diff --git a/src/client/components.tsx b/src/client/components.tsx index 3d7e605..b8b9206 100644 --- a/src/client/components.tsx +++ b/src/client/components.tsx @@ -28,7 +28,9 @@ export function RequireAuth({ children, returnTo }: RequireAuthProps) { useEffect(() => { if (!isLoading && !isAuthenticated) { - loginWithRedirect({ returnTo }); + loginWithRedirect({ + returnTo: returnTo ?? window.location.pathname + window.location.search + }); } }, [isAuthenticated, isLoading, loginWithRedirect, returnTo]);