Skip to content

Commit 2a815eb

Browse files
p9fwobsoriano
andauthored
fix(tanstack-react-start): Do not consume the body of the request in middleware (#7020)
Co-authored-by: Robert Soriano <sorianorobertc@gmail.com>
1 parent 79d9c5e commit 2a815eb

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

.changeset/full-horses-repair.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/tanstack-react-start': patch
3+
---
4+
5+
Fixed a bug where the `clerkMiddleware()` helper would consume the body of the request

packages/tanstack-react-start/src/server/clerkMiddleware.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { RequestState } from '@clerk/backend/internal';
2-
import { AuthStatus, constants } from '@clerk/backend/internal';
2+
import { AuthStatus, constants, createClerkRequest } from '@clerk/backend/internal';
33
import { handleNetlifyCacheInDevInstance } from '@clerk/shared/netlifyCacheHandler';
44
import type { PendingSessionOptions } from '@clerk/types';
55
import type { AnyRequestMiddleware } from '@tanstack/react-start';
@@ -8,12 +8,13 @@ import { createMiddleware, json } from '@tanstack/react-start';
88
import { clerkClient } from './clerkClient';
99
import { loadOptions } from './loadOptions';
1010
import type { ClerkMiddlewareOptions } from './types';
11-
import { getResponseClerkState } from './utils';
11+
import { getResponseClerkState, patchRequest } from './utils';
1212

1313
export const clerkMiddleware = (options?: ClerkMiddlewareOptions): AnyRequestMiddleware => {
1414
return createMiddleware().server(async args => {
15-
const loadedOptions = loadOptions(args.request, options);
16-
const requestState = await clerkClient().authenticateRequest(args.request, {
15+
const clerkRequest = createClerkRequest(patchRequest(args.request));
16+
const loadedOptions = loadOptions(clerkRequest, options);
17+
const requestState = await clerkClient().authenticateRequest(clerkRequest, {
1718
...loadedOptions,
1819
acceptsToken: 'any',
1920
});

packages/tanstack-react-start/src/server/loadOptions.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createClerkRequest } from '@clerk/backend/internal';
1+
import type { ClerkRequest } from '@clerk/backend/internal';
22
import { apiUrlFromPublishableKey } from '@clerk/shared/apiUrlFromPublishableKey';
33
import { getEnvVariable } from '@clerk/shared/getEnvVariable';
44
import { isDevelopmentFromSecretKey } from '@clerk/shared/keys';
@@ -9,10 +9,8 @@ import { errorThrower } from '../utils';
99
import { getPublicEnvVariables } from '../utils/env';
1010
import { commonEnvs } from './constants';
1111
import type { LoaderOptions } from './types';
12-
import { patchRequest } from './utils';
1312

14-
export const loadOptions = (request: Request, overrides: LoaderOptions = {}) => {
15-
const clerkRequest = createClerkRequest(patchRequest(request));
13+
export const loadOptions = (request: ClerkRequest, overrides: LoaderOptions = {}) => {
1614
const commonEnv = commonEnvs();
1715
const secretKey = overrides.secretKey || commonEnv.SECRET_KEY;
1816
const machineSecretKey = overrides.machineSecretKey || commonEnv.MACHINE_SECRET_KEY;
@@ -21,15 +19,15 @@ export const loadOptions = (request: Request, overrides: LoaderOptions = {}) =>
2119
const apiUrl = getEnvVariable('CLERK_API_URL') || apiUrlFromPublishableKey(publishableKey);
2220
const domain = handleValueOrFn(overrides.domain, new URL(request.url)) || commonEnv.DOMAIN;
2321
const isSatellite = handleValueOrFn(overrides.isSatellite, new URL(request.url)) || commonEnv.IS_SATELLITE;
24-
const relativeOrAbsoluteProxyUrl = handleValueOrFn(overrides?.proxyUrl, clerkRequest.clerkUrl, commonEnv.PROXY_URL);
22+
const relativeOrAbsoluteProxyUrl = handleValueOrFn(overrides?.proxyUrl, request.clerkUrl, commonEnv.PROXY_URL);
2523
const signInUrl = overrides.signInUrl || commonEnv.SIGN_IN_URL;
2624
const signUpUrl = overrides.signUpUrl || commonEnv.SIGN_UP_URL;
2725
const afterSignInUrl = overrides.afterSignInUrl || getPublicEnvVariables().afterSignInUrl;
2826
const afterSignUpUrl = overrides.afterSignUpUrl || getPublicEnvVariables().afterSignUpUrl;
2927

3028
let proxyUrl;
3129
if (!!relativeOrAbsoluteProxyUrl && isProxyUrlRelative(relativeOrAbsoluteProxyUrl)) {
32-
proxyUrl = new URL(relativeOrAbsoluteProxyUrl, clerkRequest.clerkUrl).toString();
30+
proxyUrl = new URL(relativeOrAbsoluteProxyUrl, request.clerkUrl).toString();
3331
} else {
3432
proxyUrl = relativeOrAbsoluteProxyUrl;
3533
}

0 commit comments

Comments
 (0)