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

fix(tanstack-start): Fix ENV variable loading #4743

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
39 changes: 15 additions & 24 deletions packages/tanstack-start/src/server/clerkClient.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
import { createClerkClient } from '@clerk/backend';

import {
API_URL,
API_VERSION,
DOMAIN,
IS_SATELLITE,
PROXY_URL,
PUBLISHABLE_KEY,
SDK_METADATA,
SECRET_KEY,
TELEMETRY_DEBUG,
TELEMETRY_DISABLED,
} from './constants';
import { commonEnvs } from './constants';

const clerkClient: typeof createClerkClient = options =>
createClerkClient({
secretKey: SECRET_KEY,
publishableKey: PUBLISHABLE_KEY,
apiUrl: API_URL,
apiVersion: API_VERSION,
const clerkClient: typeof createClerkClient = options => {
const commonEnv = commonEnvs();
return createClerkClient({
secretKey: commonEnv.SECRET_KEY,
publishableKey: commonEnv.PUBLISHABLE_KEY,
apiUrl: commonEnv.API_URL,
apiVersion: commonEnv.API_VERSION,
userAgent: `${PACKAGE_NAME}@${PACKAGE_VERSION}`,
proxyUrl: PROXY_URL,
domain: DOMAIN,
isSatellite: IS_SATELLITE,
sdkMetadata: SDK_METADATA,
proxyUrl: commonEnv.PROXY_URL,
domain: commonEnv.DOMAIN,
isSatellite: commonEnv.IS_SATELLITE,
sdkMetadata: commonEnv.SDK_METADATA,
telemetry: {
disabled: TELEMETRY_DISABLED,
debug: TELEMETRY_DEBUG,
disabled: commonEnv.TELEMETRY_DISABLED,
debug: commonEnv.TELEMETRY_DEBUG,
},
...options,
});
};

export { clerkClient };
45 changes: 24 additions & 21 deletions packages/tanstack-start/src/server/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@ import { apiUrlFromPublishableKey } from '@clerk/shared/apiUrlFromPublishableKey

import { getEnvVariable, getPublicEnvVariables } from '../utils/env';

export const CLERK_JS_VERSION = getPublicEnvVariables().clerkJsVersion || '';
export const CLERK_JS_URL = getPublicEnvVariables().clerkJsUrl || '';
export const API_VERSION = getEnvVariable('CLERK_API_VERSION') || 'v1';
export const SECRET_KEY = getEnvVariable('CLERK_SECRET_KEY') || '';
export const PUBLISHABLE_KEY = getPublicEnvVariables().publishableKey || '';
export const ENCRYPTION_KEY = getEnvVariable('CLERK_ENCRYPTION_KEY') || '';
export const API_URL = getEnvVariable('CLERK_API_URL') || apiUrlFromPublishableKey(PUBLISHABLE_KEY);
export const DOMAIN = getPublicEnvVariables().domain || '';
export const PROXY_URL = getPublicEnvVariables().proxyUrl || '';
export const CLERK_JWT_KEY = getEnvVariable('CLERK_JWT_KEY') || '';
export const IS_SATELLITE = getPublicEnvVariables().isSatellite || false;
export const SIGN_IN_URL = getPublicEnvVariables().signInUrl || '';
export const SIGN_UP_URL = getPublicEnvVariables().signUpUrl || '';
export const SDK_METADATA = {
name: PACKAGE_NAME,
version: PACKAGE_VERSION,
environment: getEnvVariable('NODE_ENV'),
};

export const TELEMETRY_DISABLED = getPublicEnvVariables().telemetryDisabled;
export const TELEMETRY_DEBUG = getPublicEnvVariables().telemetryDebug;
// @ts-expect-error - TODO: Improve types
export const commonEnvs = () =>
({
CLERK_JS_VERSION: getPublicEnvVariables().clerkJsVersion || '',
CLERK_JS_URL: getPublicEnvVariables().clerkJsUrl || '',
API_VERSION: getEnvVariable('CLERK_API_VERSION') || 'v1',
SECRET_KEY: getEnvVariable('CLERK_SECRET_KEY') || '',
PUBLISHABLE_KEY: getPublicEnvVariables().publishableKey || '',
ENCRYPTION_KEY: getEnvVariable('CLERK_ENCRYPTION_KEY') || '',
API_URL: getEnvVariable('CLERK_API_URL') || apiUrlFromPublishableKey(commonEnvs().PUBLISHABLE_KEY),
DOMAIN: getPublicEnvVariables().domain || '',
PROXY_URL: getPublicEnvVariables().proxyUrl || '',
CLERK_JWT_KEY: getEnvVariable('CLERK_JWT_KEY') || '',
IS_SATELLITE: getPublicEnvVariables().isSatellite || false,
SIGN_IN_URL: getPublicEnvVariables().signInUrl || '',
SIGN_UP_URL: getPublicEnvVariables().signUpUrl || '',
SDK_METADATA: {
name: PACKAGE_NAME,
version: PACKAGE_VERSION,
environment: getEnvVariable('NODE_ENV'),
},
TELEMETRY_DISABLED: getPublicEnvVariables().telemetryDisabled,
TELEMETRY_DEBUG: getPublicEnvVariables().telemetryDebug,
}) as const;
29 changes: 10 additions & 19 deletions packages/tanstack-start/src/server/loadOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,22 @@ import { handleValueOrFn } from '@clerk/shared/utils';

import { errorThrower } from '../utils';
import { getEnvVariable, getPublicEnvVariables } from '../utils/env';
import {
CLERK_JWT_KEY,
DOMAIN,
IS_SATELLITE,
PROXY_URL,
PUBLISHABLE_KEY,
SECRET_KEY,
SIGN_IN_URL,
SIGN_UP_URL,
} from './constants';
import { commonEnvs } from './constants';
import type { LoaderOptions } from './types';
import { patchRequest } from './utils';

export const loadOptions = (request: Request, overrides: LoaderOptions = {}) => {
const clerkRequest = createClerkRequest(patchRequest(request));

const secretKey = overrides.secretKey || SECRET_KEY;
const publishableKey = overrides.publishableKey || PUBLISHABLE_KEY;
const jwtKey = overrides.jwtKey || CLERK_JWT_KEY;
const commonEnv = commonEnvs();
const secretKey = overrides.secretKey || commonEnv.SECRET_KEY;
const publishableKey = overrides.publishableKey || commonEnv.PUBLISHABLE_KEY;
const jwtKey = overrides.jwtKey || commonEnv.CLERK_JWT_KEY;
const apiUrl = getEnvVariable('CLERK_API_URL') || apiUrlFromPublishableKey(publishableKey);
const domain = handleValueOrFn(overrides.domain, new URL(request.url)) || DOMAIN;
const isSatellite = handleValueOrFn(overrides.isSatellite, new URL(request.url)) || IS_SATELLITE;
const relativeOrAbsoluteProxyUrl = handleValueOrFn(overrides?.proxyUrl, clerkRequest.clerkUrl, PROXY_URL);
const signInUrl = overrides.signInUrl || SIGN_IN_URL;
const signUpUrl = overrides.signUpUrl || SIGN_UP_URL;
const domain = handleValueOrFn(overrides.domain, new URL(request.url)) || commonEnv.DOMAIN;
const isSatellite = handleValueOrFn(overrides.isSatellite, new URL(request.url)) || commonEnv.IS_SATELLITE;
const relativeOrAbsoluteProxyUrl = handleValueOrFn(overrides?.proxyUrl, clerkRequest.clerkUrl, commonEnv.PROXY_URL);
const signInUrl = overrides.signInUrl || commonEnv.SIGN_IN_URL;
const signUpUrl = overrides.signUpUrl || commonEnv.SIGN_UP_URL;
const afterSignInUrl = overrides.afterSignInUrl || getPublicEnvVariables().afterSignInUrl;
const afterSignUpUrl = overrides.afterSignUpUrl || getPublicEnvVariables().afterSignUpUrl;

Expand Down
Loading