diff --git a/packages/nextjs/src/server/constants.ts b/packages/nextjs/src/server/constants.ts index 03d39b73d18..880dd653d4c 100644 --- a/packages/nextjs/src/server/constants.ts +++ b/packages/nextjs/src/server/constants.ts @@ -1,5 +1,13 @@ +import { apiUrlFromPublishableKey } from '@clerk/shared'; import { deprecated } from '@clerk/shared/deprecated'; +export const PUBLISHABLE_KEY = process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY || ''; +export const DOMAIN = process.env.NEXT_PUBLIC_CLERK_DOMAIN || ''; +export const PROXY_URL = process.env.NEXT_PUBLIC_CLERK_PROXY_URL || ''; +export const IS_SATELLITE = process.env.NEXT_PUBLIC_CLERK_IS_SATELLITE === 'true' || false; +export const SIGN_IN_URL = process.env.NEXT_PUBLIC_CLERK_SIGN_IN_URL || ''; +export const SIGN_UP_URL = process.env.NEXT_PUBLIC_CLERK_SIGN_UP_URL || ''; + /** * @deprecated Use `CLERK_JS_VERSION` instead. */ @@ -9,7 +17,7 @@ if (JS_VERSION) { } export const CLERK_JS_VERSION = process.env.NEXT_PUBLIC_CLERK_JS_VERSION || ''; export const CLERK_JS_URL = process.env.NEXT_PUBLIC_CLERK_JS || ''; -export const API_URL = process.env.CLERK_API_URL || 'https://api.clerk.com'; +export const API_URL = process.env.CLERK_API_URL || apiUrlFromPublishableKey(PUBLISHABLE_KEY); export const API_VERSION = process.env.CLERK_API_VERSION || 'v1'; /** * @deprecated Use `CLERK_SECRET_KEY` instead. @@ -26,9 +34,3 @@ export const FRONTEND_API = process.env.NEXT_PUBLIC_CLERK_FRONTEND_API || ''; if (FRONTEND_API) { deprecated('NEXT_PUBLIC_CLERK_FRONTEND_API', 'Use `NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY` environment variable instead.'); } -export const PUBLISHABLE_KEY = process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY || ''; -export const DOMAIN = process.env.NEXT_PUBLIC_CLERK_DOMAIN || ''; -export const PROXY_URL = process.env.NEXT_PUBLIC_CLERK_PROXY_URL || ''; -export const IS_SATELLITE = process.env.NEXT_PUBLIC_CLERK_IS_SATELLITE === 'true' || false; -export const SIGN_IN_URL = process.env.NEXT_PUBLIC_CLERK_SIGN_IN_URL || ''; -export const SIGN_UP_URL = process.env.NEXT_PUBLIC_CLERK_SIGN_UP_URL || ''; diff --git a/packages/shared/src/constants.ts b/packages/shared/src/constants.ts new file mode 100644 index 00000000000..3168d86229f --- /dev/null +++ b/packages/shared/src/constants.ts @@ -0,0 +1,11 @@ +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', 'accounts.dev']; + +export const DEV_OR_STAGING_SUFFIXES = [...DEV_SUFFIXES, ...STAGING_SUFFIXES]; diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts index b5d8d07b76e..3ef8a015083 100644 --- a/packages/shared/src/index.ts +++ b/packages/shared/src/index.ts @@ -10,7 +10,6 @@ export * from './utils'; -export { createWorkerTimers } from './workerTimers'; export * from './browser'; export { callWithRetry } from './callWithRetry'; export * from './color'; @@ -27,3 +26,4 @@ export * from './poller'; export * from './proxy'; export * from './underscore'; export * from './url'; +export { createWorkerTimers } from './workerTimers'; diff --git a/packages/shared/src/keys.ts b/packages/shared/src/keys.ts index d2f4848f12c..5d7c690ef24 100644 --- a/packages/shared/src/keys.ts +++ b/packages/shared/src/keys.ts @@ -1,5 +1,6 @@ import type { PublishableKey } from '@clerk/types'; +import { DEV_OR_STAGING_SUFFIXES } from './constants'; import { isomorphicAtob } from './isomorphicAtob'; const PUBLISHABLE_KEY_LIVE_PREFIX = 'pk_live_'; @@ -56,18 +57,6 @@ 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(); return { diff --git a/packages/shared/src/utils/apiUrlFromPublishableKey.ts b/packages/shared/src/utils/apiUrlFromPublishableKey.ts new file mode 100644 index 00000000000..15c8fe14745 --- /dev/null +++ b/packages/shared/src/utils/apiUrlFromPublishableKey.ts @@ -0,0 +1,13 @@ +import { DEV_SUFFIXES, STAGING_SUFFIXES } from '../constants'; +import { parsePublishableKey } from '../keys'; + +export const apiUrlFromPublishableKey = (publishableKey: string) => { + const frontendApi = parsePublishableKey(publishableKey)?.frontendApi; + if (DEV_SUFFIXES.some(suffix => frontendApi?.endsWith(suffix))) { + return 'https://api.lclclerk.com'; + } + if (STAGING_SUFFIXES.some(suffix => frontendApi?.endsWith(suffix))) { + return 'https://api.clerkstage.dev'; + } + return 'https://api.clerk.com'; +}; diff --git a/packages/shared/src/utils/index.ts b/packages/shared/src/utils/index.ts index 8838c7ed4b4..dc5cafd53a8 100644 --- a/packages/shared/src/utils/index.ts +++ b/packages/shared/src/utils/index.ts @@ -1,6 +1,7 @@ +export { apiUrlFromPublishableKey } from './apiUrlFromPublishableKey'; export * from './createDeferredPromise'; export { isStaging } from './instance'; +export { logErrorInDevMode } from './logErrorInDevMode'; export { noop } from './noop'; -export * from './runtimeEnvironment'; export * from './runWithExponentialBackOff'; -export { logErrorInDevMode } from './logErrorInDevMode'; +export * from './runtimeEnvironment';