Skip to content

Commit

Permalink
chore(clerk-sdk-node): Move loading env as default value in authentic…
Browse files Browse the repository at this point in the history
…ateRequest
  • Loading branch information
dimkl committed Jun 12, 2023
1 parent 263b3e4 commit eff4e45
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-avocados-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-sdk-node': patch
---

Load env variables upon first usage of middlewares or clerkClient
2 changes: 1 addition & 1 deletion packages/sdk-node/src/authenticateRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('authenticateRequest', () => {
referrer: 'referer',
userAgent: 'user-agent',
isSatellite: false,
proxyUrl: undefined,
proxyUrl: '',
signInUrl: '',
domain: '',
searchParams,
Expand Down
15 changes: 9 additions & 6 deletions packages/sdk-node/src/authenticateRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { constants } from '@clerk/backend';
import cookie from 'cookie';
import type { IncomingMessage, ServerResponse } from 'http';

import { loadApiEnv, loadClientEnv } from './clerkClient';
import { handleValueOrFn, isHttpOrHttps, isProxyUrlRelative, isValidProxyUrl } from './shared';
import type { ClerkMiddlewareOptions } from './types';

Expand Down Expand Up @@ -46,16 +47,16 @@ export const authenticateRequest = (opts: {
options?: ClerkMiddlewareOptions;
}) => {
const { clerkClient, apiKey, secretKey, frontendApi, publishableKey, req, options } = opts;
const cookies = parseCookies(req);
const { jwtKey, authorizedParties, audience } = options || {};

const env = { ...loadApiEnv(), ...loadClientEnv() };

const requestUrl = getRequestUrl(req);
const isSatellite =
handleValueOrFn(options?.isSatellite, requestUrl) || process.env.CLERK_IS_SATELLITE === 'true' || false;
const domain = handleValueOrFn(options?.domain, requestUrl) || process.env.CLERK_DOMAIN || '';
const signInUrl = options?.signInUrl || process.env.CLERK_SIGN_IN_URL || '';
const isSatellite = handleValueOrFn(options?.isSatellite, requestUrl, env.isSatellite);
const domain = handleValueOrFn(options?.domain, requestUrl) || env.domain;
const signInUrl = options?.signInUrl || env.signInUrl;
const proxyUrl = absoluteProxyUrl(
handleValueOrFn(options?.proxyUrl, requestUrl, process.env.CLERK_PROXY_URL) as string,
handleValueOrFn(options?.proxyUrl, requestUrl, env.proxyUrl),
requestUrl.toString(),
);

Expand All @@ -67,6 +68,8 @@ export const authenticateRequest = (opts: {
throw new Error(satelliteAndMissingSignInUrl);
}

const cookies = parseCookies(req);

return clerkClient.authenticateRequest({
audience,
apiKey,
Expand Down
5 changes: 2 additions & 3 deletions packages/sdk-node/src/requireAuth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Request, Response } from 'express';

import { clerkClient, loadApiEnv, loadClientEnv } from './clerkClient';
import { clerkClient } from './clerkClient';
import { createClerkExpressRequireAuth } from './clerkExpressRequireAuth';
import type { ClerkMiddlewareOptions, RequireAuthProp } from './types';
import { runMiddleware } from './utils';
Expand All @@ -9,8 +9,7 @@ type ExpressApiHandlerRequireAuth<T = any> = (req: RequireAuthProp<Request>, res

export function requireAuth(handler: ExpressApiHandlerRequireAuth, options?: ClerkMiddlewareOptions): any {
return async (req: Request, res: Response) => {
const env = { ...loadApiEnv(), ...loadClientEnv() };
await runMiddleware(req, res, createClerkExpressRequireAuth({ clerkClient, ...env })(options));
await runMiddleware(req, res, createClerkExpressRequireAuth({ clerkClient })(options));

return handler(req as RequireAuthProp<Request>, res);
};
Expand Down
5 changes: 2 additions & 3 deletions packages/sdk-node/src/withAuth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Request, Response } from 'express';

import { clerkClient, loadApiEnv, loadClientEnv } from './clerkClient';
import { clerkClient } from './clerkClient';
import { createClerkExpressWithAuth } from './clerkExpressWithAuth';
import type { ClerkMiddlewareOptions, WithAuthProp } from './types';
import { runMiddleware } from './utils';
Expand All @@ -13,8 +13,7 @@ export function withAuth<TRequest extends Request = Request, TResponse extends R
options?: ClerkMiddlewareOptions,
): any {
return async (req: TRequest, res: TResponse) => {
const env = { ...loadApiEnv(), ...loadClientEnv() };
await runMiddleware(req, res, createClerkExpressWithAuth({ clerkClient, ...env })(options));
await runMiddleware(req, res, createClerkExpressWithAuth({ clerkClient })(options));

return handler(req as WithAuthProp<TRequest>, res);
};
Expand Down

0 comments on commit eff4e45

Please sign in to comment.