Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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/fifty-buses-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/tanstack-react-start': patch
---

Fix serialization errors during handshake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AuthStatus, constants } from '@clerk/backend/internal';
import { handleNetlifyCacheInDevInstance } from '@clerk/shared/netlifyCacheHandler';

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

export async function authenticateRequest(
Expand Down Expand Up @@ -41,9 +42,9 @@ export async function authenticateRequest(
requestStateHeaders: requestState.headers,
publishableKey: requestState.publishableKey,
});

// triggering a handshake redirect
// eslint-disable-next-line @typescript-eslint/only-throw-error
throw new Response(null, { status: 307, headers: requestState.headers });
throw new ClerkHandshakeRedirect(307, requestState.headers);
}

if (requestState.status === AuthStatus.Handshake) {
Expand Down
11 changes: 11 additions & 0 deletions packages/tanstack-react-start/src/server/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class ClerkHandshakeRedirect extends Error {
constructor(
public status: number,
public headers: Headers,
) {
super('Clerk handshake redirect required');
this.name = 'ClerkHandshakeRedirect';
this.status = status;
this.headers = headers;
}
}
8 changes: 6 additions & 2 deletions packages/tanstack-react-start/src/server/middlewareHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { AnyRouter } from '@tanstack/react-router';
import type { CustomizeStartHandler, HandlerCallback, RequestHandler } from '@tanstack/react-start/server';

import { authenticateRequest } from './authenticateRequest';
import { ClerkHandshakeRedirect } from './errors';
import { loadOptions } from './loadOptions';
import type { LoaderOptions } from './types';
import { getResponseClerkState } from './utils';
Expand Down Expand Up @@ -33,9 +34,12 @@ export function createClerkHandler<TRouter extends AnyRouter>(

await router.load();
} catch (error) {
if (error instanceof Response) {
if (error instanceof ClerkHandshakeRedirect) {
// returning the response
return error;
return new Response(null, {
status: error.status,
headers: error.headers,
});
}

// rethrowing the error if it is not a Response
Expand Down
Loading