Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions packages/clerk-js/src/core/__tests__/clerk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2015,6 +2015,66 @@ describe('Clerk singleton', () => {
expect(mockNavigate.mock.calls[0][0]).toBe('/sign-in#/reset-password');
});
});

it('reads redirect_url from URL search params and uses it as the redirect destination', async () => {
// Set window.location to include redirect_url in search params
const customRedirectUrl = '/custom-redirect-from-url';
Object.defineProperty(global.window, 'location', {
value: {
...mockWindowLocation,
search: `?redirect_url=${encodeURIComponent(customRedirectUrl)}`,
},
});

mockEnvironmentFetch.mockReturnValue(
Promise.resolve({
authConfig: {},
userSettings: mockUserSettings,
displayConfig: mockDisplayConfig,
isSingleSession: () => false,
isProduction: () => false,
isDevelopmentOrStaging: () => true,
onWindowLocationHost: () => false,
}),
);

mockClientFetch.mockReturnValue(
Promise.resolve({
signedInSessions: [],
signIn: new SignIn({
status: 'complete',
first_factor_verification: {
status: 'verified',
strategy: 'oauth_google',
external_verification_redirect_url: null,
error: null,
},
second_factor_verification: null,
identifier: '[email protected]',
user_data: null,
created_session_id: 'sess_123',
} as any as SignInJSON),
signUp: new SignUp(null),
}),
);

const mockSetActive = vi.fn(async ({ navigate }) => {
// Simulate the navigate callback being called
await navigate({ session: { currentTask: null } });
});

const sut = new Clerk(productionPublishableKey);
await sut.load(mockedLoadOptions);
sut.setActive = mockSetActive;

await sut.handleRedirectCallback();

await waitFor(() => {
expect(mockSetActive).toHaveBeenCalled();
// The redirect should include the custom redirect URL from URL search params
expect(mockNavigate.mock.calls[0][0]).toContain(customRedirectUrl);
});
});
});

describe('.handleEmailLinkVerification()', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2084,7 +2084,7 @@ export class Clerk implements ClerkInterface {
buildURL({ base: displayConfig.signInUrl, hashPath: '/reset-password' }, { stringify: true }),
);

const redirectUrls = new RedirectUrls(this.#options, params);
const redirectUrls = new RedirectUrls(this.#options, params, new URLSearchParams(window.location.search));

const navigateToContinueSignUp = makeNavigate(
params.continueSignUpUrl ||
Expand Down
Loading