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/puny-brooms-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/tanstack-react-start': patch
---

Apply Clerk response headers
10 changes: 10 additions & 0 deletions integration/tests/tanstack-start/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,15 @@ testAgainstRunningApps({ withEnv: [appConfigs.envs.withEmailCodes] })(

expect(clerkInitialState !== undefined).toBeTruthy();
});

test('clerk handler sets headers', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
const r = await u.po.signIn.goTo();

expect(r.headers()).toContain({
'x-clerk-auth-reason': 'session-token-and-uat-missing',
'x-clerk-auth-status': 'signed-out',
});
});
},
);
9 changes: 7 additions & 2 deletions packages/tanstack-react-start/src/server/middlewareHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ export function createClerkHandler<TRouter extends AnyRouter>(

const requestState = await authenticateRequest(request, loadedOptions);

const clerkInitialState = getResponseClerkState(requestState, loadedOptions);
const { clerkInitialState, headers } = getResponseClerkState(requestState, loadedOptions);

// Merging the TanStack router context with the Clerk context and loading the router
router.update({
context: { ...router.options.context, ...clerkInitialState },
context: { ...router.options.context, clerkInitialState },
});

// Adding the Clerk response headers to the response
headers.forEach((value, key) => {
responseHeaders.set(key, value);
});

await router.load();
Expand Down
Loading