Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(clerk-js): Throw error if routing options are used incorrectly #2208

Merged
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
10 changes: 10 additions & 0 deletions .changeset/brave-suits-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@clerk/clerk-js': major
---

All the components that using routing will throw a runtime error if the a path property is provided with a routing strategy other than path.

Example that will throw an error:
```tsx
<SignIn routing='hash' path='/sign-in' />
```
4 changes: 4 additions & 0 deletions packages/clerk-js/src/core/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,7 @@ export function clerkRedirectUrlIsMissingScheme(): never {
export function clerkFailedToLoadThirdPartyScript(name?: string): never {
throw new Error(`${errorPrefix} Unable to retrieve a third party script${name ? ` ${name}` : ''}.`);
}

export function clerkInvalidRoutingStrategy(strategy?: string): never {
throw new Error(`${errorPrefix} Invalid routing strategy, path cannot be used in tandem with ${strategy}.`);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ describe('auth prop helpers', () => {
expect(normalizeRoutingOptions({ path: 'test' })).toEqual({ routing: 'path', path: 'test' });
});

it('returns the same options if routing was provided (any routing) and path was provided (avoid breaking integrations)', () => {
expect(normalizeRoutingOptions({ routing: 'path', path: 'test' })).toEqual({ routing: 'path', path: 'test' });
expect(normalizeRoutingOptions({ routing: 'hash', path: 'test' })).toEqual({ routing: 'hash', path: 'test' });
expect(normalizeRoutingOptions({ routing: 'virtual' })).toEqual({ routing: 'virtual' });
it('it throws an error when path is provided and routing strategy is not path', () => {
expect(() => {
normalizeRoutingOptions({ path: 'test', routing: 'hash' });
}).toThrow('ClerkJS: Invalid routing strategy, path cannot be used in tandem with hash.');
expect(() => {
normalizeRoutingOptions({ path: 'test', routing: 'virtual' });
}).toThrow('ClerkJS: Invalid routing strategy, path cannot be used in tandem with virtual.');
});
});
});
5 changes: 5 additions & 0 deletions packages/clerk-js/src/utils/authPropHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ClerkOptions, DisplayConfigResource, RoutingOptions, RoutingStrate
import type { ParsedQs } from 'qs';
import qs from 'qs';

import { clerkInvalidRoutingStrategy } from '../core/errors';
import { hasBannedProtocol, isAllowedRedirectOrigin, isValidUrl } from './url';

type PickRedirectionUrlKey = 'afterSignUpUrl' | 'afterSignInUrl' | 'signInUrl' | 'signUpUrl';
Expand Down Expand Up @@ -124,5 +125,9 @@ export const normalizeRoutingOptions = ({
return { routing: 'path', path };
}

if (routing !== 'path' && !!path) {
return clerkInvalidRoutingStrategy(routing);
}

return { routing, path } as RoutingOptions;
};
Loading