diff --git a/.changeset/full-frogs-play.md b/.changeset/full-frogs-play.md new file mode 100644 index 00000000000..a57599fba0b --- /dev/null +++ b/.changeset/full-frogs-play.md @@ -0,0 +1,5 @@ +--- +'@clerk/backend': patch +--- + +The `instanceof` check in `createClerkRequest` is allowing `Request` instances through, which means the required `cookies` object is never created/set on the instance. This causes an error in the TanStack Start middleware when `AuthenticateContext.getCookie` tries to access `cookies`. Instead of checking `instanceof`, we check for the presence of the `cookies` and `clerkUrl` keys to determine whether or not we're dealing with a `Request` or `ClerkRequest` diff --git a/packages/backend/src/tokens/clerkRequest.ts b/packages/backend/src/tokens/clerkRequest.ts index f35b079d779..d17576af50e 100644 --- a/packages/backend/src/tokens/clerkRequest.ts +++ b/packages/backend/src/tokens/clerkRequest.ts @@ -82,7 +82,12 @@ class ClerkRequest extends Request { } export const createClerkRequest = (...args: ConstructorParameters): ClerkRequest => { - return args[0] instanceof ClerkRequest ? args[0] : new ClerkRequest(...args); + const isClerkRequest = args[0] && + typeof args[0] === 'object' && + 'clerkUrl' in args[0] && + 'cookies' in args[0]; + + return isClerkRequest ? args[0] as ClerkRequest : new ClerkRequest(...args); }; export type { ClerkRequest };