Skip to content

Commit

Permalink
feat(clerk-js): Throw error if routing options are used incorrectly (#…
Browse files Browse the repository at this point in the history
…2208)

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

* test(clerk-js): Update normalizeRoutingOptions tests

* chore(repo): Added Changeset
  • Loading branch information
octoper authored Nov 24, 2023
1 parent 2869d75 commit 043801f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
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;
};

0 comments on commit 043801f

Please sign in to comment.