Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/purple-rockets-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/tanstack-start": patch
---

Fix a bug when using the `getAuth` function multiple times
3 changes: 2 additions & 1 deletion packages/tanstack-start/src/server/authenticateRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { AuthenticateRequestOptions, SignedInState, SignedOutState } from '
import { AuthStatus } from '@clerk/backend/internal';

import { errorThrower } from '../utils';
import { patchRequest } from './utils';

export async function authenticateRequest(
request: Request,
Expand All @@ -22,7 +23,7 @@ export async function authenticateRequest(
domain,
publishableKey,
userAgent: `${PACKAGE_NAME}@${PACKAGE_VERSION}`,
}).authenticateRequest(request, {
}).authenticateRequest(patchRequest(request), {
audience,
authorizedParties,
signInUrl,
Expand Down
3 changes: 2 additions & 1 deletion packages/tanstack-start/src/server/loadOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import { isTruthy } from '@clerk/shared/underscore';
import { errorThrower } from '../utils';
import { getEnvVariable, getPublicEnvVariables } from '../utils/env';
import type { LoaderOptions } from './types';
import { patchRequest } from './utils';

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

const secretKey = overrides.secretKey || overrides.secretKey || getEnvVariable('CLERK_SECRET_KEY');
const publishableKey = overrides.publishableKey || getPublicEnvVariables().publishableKey;
Expand Down
24 changes: 24 additions & 0 deletions packages/tanstack-start/src/server/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,27 @@ export function getResponseClerkState(requestState: RequestState, additionalStat
headers: requestState.headers,
};
}

/**
* Patches request to avoid duplex issues with unidici
* For more information, see:
* https://github.com/nodejs/node/issues/46221
* https://github.com/whatwg/fetch/pull/1457
* @internal
*/
export const patchRequest = (request: Request) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@octoper Would it make sense or is it possible to have this be part of createClerkRequest ?

I remember having to do something similar for remix.

@octoper octoper Aug 27, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah need we need to revise it in the @clerk/backend how we handle request, I will add ticket so we can have a better look at it

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious if adding this to createClerkRequest would break other SDKs like Next

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was not sure about it, I created a ticket so we can have a better look at it and have one solution for everything

const clonedRequest = new Request(request.url, {
headers: request.headers,
method: request.method,
redirect: request.redirect,
cache: request.cache,
signal: request.signal,
});

// If duplex is not set, set it to 'half' to avoid duplex issues with unidici
if (clonedRequest.method !== 'GET' && clonedRequest.body !== null && !('duplex' in clonedRequest)) {
(clonedRequest as unknown as { duplex: 'half' }).duplex = 'half';
}

return clonedRequest;
};