Skip to content

Commit

Permalink
refactor(clerk-js,nextjs,shared): Move account portal utils to shared…
Browse files Browse the repository at this point in the history
… package
  • Loading branch information
desiprisg committed Oct 31, 2023
1 parent 86133f1 commit 1318b2f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 65 deletions.
32 changes: 6 additions & 26 deletions packages/clerk-js/src/utils/url.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { camelToSnake, createDevOrStagingUrlCache } from '@clerk/shared';
import {
camelToSnake,
createDevOrStagingUrlCache,
isCurrentDevAccountPortalOrigin,
isLegacyDevAccountPortalOrigin,
} from '@clerk/shared';
import { globs } from '@clerk/shared/globs';
import type { SignUpResource } from '@clerk/types';

Expand Down Expand Up @@ -28,9 +33,6 @@ export const DEV_OR_STAGING_SUFFIXES = [
'accounts.dev',
];

export const LEGACY_DEV_SUFFIXES = ['.lcl.dev', '.lclstage.dev', '.lclclerk.com'];
export const CURRENT_DEV_SUFFIXES = ['.accounts.dev', '.accountsstage.dev', '.accounts.lclclerk.com'];

const BANNED_URI_PROTOCOLS = ['javascript:'] as const;

const { isDevOrStagingUrl } = createDevOrStagingUrlCache();
Expand All @@ -52,28 +54,6 @@ export function isDevAccountPortalOrigin(hostname: string = window.location.host
return res;
}

// Returns true for hosts such as:
// * accounts.foo.bar-13.lcl.dev
// * accounts.foo.bar-13.lclstage.dev
// * accounts.foo.bar-13.dev.lclclerk.com
function isLegacyDevAccountPortalOrigin(host: string): boolean {
return LEGACY_DEV_SUFFIXES.some(legacyDevSuffix => {
return host.startsWith('accounts.') && host.endsWith(legacyDevSuffix);
});
}

// Returns true for hosts such as:
// * foo-bar-13.accounts.dev
// * foo-bar-13.accountsstage.dev
// * foo-bar-13.accounts.lclclerk.com
// But false for:
// * foo-bar-13.clerk.accounts.lclclerk.com
function isCurrentDevAccountPortalOrigin(host: string): boolean {
return CURRENT_DEV_SUFFIXES.some(currentDevSuffix => {
return host.endsWith(currentDevSuffix) && !host.endsWith('.clerk' + currentDevSuffix);
});
}

export function getETLDPlusOneFromFrontendApi(frontendApi: string): string {
return frontendApi.replace('clerk.', '');
}
Expand Down
28 changes: 1 addition & 27 deletions packages/nextjs/src/server/url.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
// TODO: This is a partial duplicate of part of packages/clerk-js/src/utils/url.ts
// TODO: To be removed when we can extract this utility to @clerk/shared

export const LEGACY_DEV_SUFFIXES = ['.lcl.dev', '.lclstage.dev', '.lclclerk.com'];
export const CURRENT_DEV_SUFFIXES = ['.accounts.dev', '.accountsstage.dev', '.accounts.lclclerk.com'];
import { isCurrentDevAccountPortalOrigin, isLegacyDevAccountPortalOrigin } from '@clerk/shared';

const accountPortalCache = new Map<string, boolean>();

Expand All @@ -20,25 +16,3 @@ export function isDevAccountPortalOrigin(hostname: string): boolean {

return res;
}

// Returns true for hosts such as:
// * accounts.foo.bar-13.lcl.dev
// * accounts.foo.bar-13.lclstage.dev
// * accounts.foo.bar-13.dev.lclclerk.com
function isLegacyDevAccountPortalOrigin(host: string): boolean {
return LEGACY_DEV_SUFFIXES.some(legacyDevSuffix => {
return host.startsWith('accounts.') && host.endsWith(legacyDevSuffix);
});
}

// Returns true for hosts such as:
// * foo-bar-13.accounts.dev
// * foo-bar-13.accountsstage.dev
// * foo-bar-13.accounts.lclclerk.com
// But false for:
// * foo-bar-13.clerk.accounts.lclclerk.com
function isCurrentDevAccountPortalOrigin(host: string): boolean {
return CURRENT_DEV_SUFFIXES.some(currentDevSuffix => {
return host.endsWith(currentDevSuffix) && !host.endsWith('.clerk' + currentDevSuffix);
});
}
13 changes: 2 additions & 11 deletions packages/shared/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
export const DEV_SUFFIXES = [
'.lcl.dev',
'.lclstage.dev',
'.dev.lclclerk.com',
'.accounts.lclclerk.com',
'.stg.lclclerk.com',
];

export const STAGING_SUFFIXES = ['.stg.dev', '.stgstage.dev', 'accountsstage.dev'];

export const DEV_OR_STAGING_SUFFIXES = [...DEV_SUFFIXES, ...STAGING_SUFFIXES];
export const LEGACY_DEV_SUFFIXES = ['.lcl.dev', '.lclstage.dev', '.lclclerk.com'];
export const CURRENT_DEV_SUFFIXES = ['.accounts.dev', '.accountsstage.dev', '.accounts.lclclerk.com'];
1 change: 1 addition & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from './utils';
export * from './browser';
export { callWithRetry } from './callWithRetry';
export * from './color';
export * from './constants';
export * from './date';
export * from './deprecated';
export * from './error';
Expand Down
13 changes: 12 additions & 1 deletion packages/shared/src/keys.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { PublishableKey } from '@clerk/types';

import { DEV_OR_STAGING_SUFFIXES } from './constants';
import { isomorphicAtob } from './isomorphicAtob';

const PUBLISHABLE_KEY_LIVE_PREFIX = 'pk_live_';
Expand Down Expand Up @@ -57,6 +56,18 @@ export function isLegacyFrontendApiKey(key: string) {

export function createDevOrStagingUrlCache() {
// TODO: Check if we can merge it with `./instance.ts#isStaging()`
const DEV_OR_STAGING_SUFFIXES = [
'.lcl.dev',
'.stg.dev',
'.lclstage.dev',
'.stgstage.dev',
'.dev.lclclerk.com',
'.stg.lclclerk.com',
'.accounts.lclclerk.com',
'accountsstage.dev',
'accounts.dev',
];

const devOrStagingUrlCache = new Map<string, boolean>();

return {
Expand Down
23 changes: 23 additions & 0 deletions packages/shared/src/url.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CURRENT_DEV_SUFFIXES, LEGACY_DEV_SUFFIXES } from './constants';
import { isStaging } from './utils/instance';

export function parseSearchParams(queryString = ''): URLSearchParams {
Expand Down Expand Up @@ -64,3 +65,25 @@ export const getScriptUrl = (
const major = getClerkJsMajorVersionOrTag(frontendApi, pkgVersion);
return `https://${noSchemeFrontendApi}/npm/@clerk/clerk-js@${clerkJSVersion || major}/dist/clerk.browser.js`;
};

// Returns true for hosts such as:
// * accounts.foo.bar-13.lcl.dev
// * accounts.foo.bar-13.lclstage.dev
// * accounts.foo.bar-13.dev.lclclerk.com
export function isLegacyDevAccountPortalOrigin(host: string): boolean {
return LEGACY_DEV_SUFFIXES.some(legacyDevSuffix => {
return host.startsWith('accounts.') && host.endsWith(legacyDevSuffix);
});
}

// Returns true for hosts such as:
// * foo-bar-13.accounts.dev
// * foo-bar-13.accountsstage.dev
// * foo-bar-13.accounts.lclclerk.com
// But false for:
// * foo-bar-13.clerk.accounts.lclclerk.com
export function isCurrentDevAccountPortalOrigin(host: string): boolean {
return CURRENT_DEV_SUFFIXES.some(currentDevSuffix => {
return host.endsWith(currentDevSuffix) && !host.endsWith('.clerk' + currentDevSuffix);
});
}

0 comments on commit 1318b2f

Please sign in to comment.