Skip to content

Commit f936cf1

Browse files
authored
feat(clerk-js): Add Frontend API URL to allowed redirect origins (#4767)
1 parent d1a8d8d commit f936cf1

File tree

4 files changed

+45
-18
lines changed

4 files changed

+45
-18
lines changed

.changeset/silver-dodos-invite.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
---
4+
5+
`createAllowedRedirectOrigins` now takes the instance type into account to include Frontend API URL for development instances. This is necessary to properly support Clerk as an IdP with OAuth for development instances.

packages/clerk-js/src/core/clerk.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -2170,7 +2170,11 @@ export class Clerk implements ClerkInterface {
21702170
return {
21712171
...defaultOptions,
21722172
...options,
2173-
allowedRedirectOrigins: createAllowedRedirectOrigins(options?.allowedRedirectOrigins, this.frontendApi),
2173+
allowedRedirectOrigins: createAllowedRedirectOrigins(
2174+
options?.allowedRedirectOrigins,
2175+
this.frontendApi,
2176+
this.instanceType,
2177+
),
21742178
};
21752179
};
21762180

packages/clerk-js/src/utils/__tests__/url.test.ts

+30-17
Original file line numberDiff line numberDiff line change
@@ -511,26 +511,39 @@ describe('isAllowedRedirect', () => {
511511
});
512512

513513
describe('createAllowedRedirectOrigins', () => {
514-
it('contains the default allowed origin values if no value is provided', async () => {
515-
const frontendApi = 'https://somename.clerk.accounts.dev';
516-
const allowedRedirectOriginsValuesUndefined = createAllowedRedirectOrigins(undefined, frontendApi);
517-
const allowedRedirectOriginsValuesEmptyArray = createAllowedRedirectOrigins([], frontendApi);
518-
519-
expect(allowedRedirectOriginsValuesUndefined).toEqual([
520-
'http://localhost',
521-
`https://${getETLDPlusOneFromFrontendApi(frontendApi)}`,
522-
`https://*.${getETLDPlusOneFromFrontendApi(frontendApi)}`,
523-
]);
524-
525-
expect(allowedRedirectOriginsValuesEmptyArray).toEqual([
526-
'http://localhost',
527-
`https://${getETLDPlusOneFromFrontendApi(frontendApi)}`,
528-
`https://*.${getETLDPlusOneFromFrontendApi(frontendApi)}`,
529-
]);
514+
it('contains the default allowed origin values if no value is provided when production instance', () => {
515+
const frontendApi = 'clerk.example.com';
516+
const allowedRedirectOriginsValuesUndefined = createAllowedRedirectOrigins(undefined, frontendApi, 'production');
517+
const allowedRedirectOriginsValuesEmptyArray = createAllowedRedirectOrigins([], frontendApi, 'production');
518+
519+
const expectedAllowedRedirectOrigins = [
520+
'http://localhost', // Current location
521+
`https://example.com`, // Primary domain
522+
`https://*.example.com`, // Wildcard subdomains
523+
];
524+
525+
expect(allowedRedirectOriginsValuesUndefined).toEqual(expectedAllowedRedirectOrigins);
526+
expect(allowedRedirectOriginsValuesEmptyArray).toEqual(expectedAllowedRedirectOrigins);
527+
});
528+
529+
it('contains the default allowed origin values and FAPI if no value is provided when development instance', () => {
530+
const frontendApi = 'foo-bar-42.clerk.accounts.dev';
531+
const allowedRedirectOriginsValuesUndefined = createAllowedRedirectOrigins(undefined, frontendApi, 'development');
532+
const allowedRedirectOriginsValuesEmptyArray = createAllowedRedirectOrigins([], frontendApi, 'development');
533+
534+
const expectedAllowedRedirectOrigins = [
535+
'http://localhost', // Current location
536+
`https://foo-bar-42.accounts.dev`, // Account Portal
537+
`https://*.foo-bar-42.accounts.dev`, // Account Portal subdomains
538+
`https://foo-bar-42.clerk.accounts.dev`, // Frontend API
539+
];
540+
541+
expect(allowedRedirectOriginsValuesUndefined).toEqual(expectedAllowedRedirectOrigins);
542+
expect(allowedRedirectOriginsValuesEmptyArray).toEqual(expectedAllowedRedirectOrigins);
530543
});
531544

532545
it('contains only the allowedRedirectOrigins options given', async () => {
533-
const frontendApi = 'https://somename.clerk.accounts.dev';
546+
const frontendApi = 'somename.clerk.accounts.dev';
534547
const allowedRedirectOriginsValues = createAllowedRedirectOrigins(
535548
['https://test.host', 'https://*.test.host'],
536549
frontendApi,

packages/clerk-js/src/utils/url.ts

+5
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ export const isAllowedRedirect =
384384
export function createAllowedRedirectOrigins(
385385
allowedRedirectOrigins: Array<string | RegExp> | undefined,
386386
frontendApi: string,
387+
instanceType?: string,
387388
): (string | RegExp)[] | undefined {
388389
if (Array.isArray(allowedRedirectOrigins) && !!allowedRedirectOrigins.length) {
389390
return allowedRedirectOrigins;
@@ -397,5 +398,9 @@ export function createAllowedRedirectOrigins(
397398
origins.push(`https://${getETLDPlusOneFromFrontendApi(frontendApi)}`);
398399
origins.push(`https://*.${getETLDPlusOneFromFrontendApi(frontendApi)}`);
399400

401+
if (instanceType === 'development') {
402+
origins.push(`https://${frontendApi}`);
403+
}
404+
400405
return origins;
401406
}

0 commit comments

Comments
 (0)