Skip to content

Commit 6f34fb7

Browse files
committed
chore(tanstack-react-start): Reuse existing auth object from server handler
1 parent 9799a44 commit 6f34fb7

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed
Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,18 @@
11
import type { AuthenticateRequestOptions, GetAuthFn } from '@clerk/backend/internal';
22
import { getAuthObjectForAcceptedToken } from '@clerk/backend/internal';
3+
import { getContext } from '@tanstack/react-start/server';
34

45
import { errorThrower } from '../utils';
56
import { noFetchFnCtxPassedInGetAuth } from '../utils/errors';
6-
import { authenticateRequest } from './authenticateRequest';
7-
import { loadOptions } from './loadOptions';
8-
import type { LoaderOptions } from './types';
97

10-
type GetAuthOptions = { acceptsToken?: AuthenticateRequestOptions['acceptsToken'] } & Pick<LoaderOptions, 'secretKey'>;
8+
type GetAuthOptions = { acceptsToken?: AuthenticateRequestOptions['acceptsToken'] };
119

12-
export const getAuth: GetAuthFn<Request, true> = (async (request: Request, opts?: GetAuthOptions) => {
10+
export const getAuth: GetAuthFn<Request> = ((request: Request, opts?: GetAuthOptions) => {
1311
if (!request) {
1412
return errorThrower.throw(noFetchFnCtxPassedInGetAuth);
1513
}
1614

17-
const { acceptsToken, ...restOptions } = opts || {};
15+
const authObjectFn = getContext('auth');
1816

19-
const loadedOptions = loadOptions(request, restOptions);
20-
21-
const requestState = await authenticateRequest(request, {
22-
...loadedOptions,
23-
acceptsToken: 'any',
24-
});
25-
26-
const authObject = requestState.toAuth();
27-
28-
return getAuthObjectForAcceptedToken({ authObject, acceptsToken });
29-
}) as GetAuthFn<Request, true>;
17+
return getAuthObjectForAcceptedToken({ authObject: authObjectFn(), acceptsToken: opts?.acceptsToken });
18+
}) as GetAuthFn<Request>;

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import type { AnyRouter } from '@tanstack/react-router';
2-
import type { CustomizeStartHandler, HandlerCallback, RequestHandler } from '@tanstack/react-start/server';
2+
import {
3+
type CustomizeStartHandler,
4+
getEvent,
5+
type HandlerCallback,
6+
type RequestHandler,
7+
} from '@tanstack/react-start/server';
38

49
import { authenticateRequest } from './authenticateRequest';
510
import { ClerkHandshakeRedirect } from './errors';
@@ -13,6 +18,8 @@ export function createClerkHandler<TRouter extends AnyRouter>(
1318
) {
1419
return (cb: HandlerCallback<TRouter>): RequestHandler => {
1520
return eventHandler(async ({ request, router, responseHeaders }) => {
21+
const event = getEvent();
22+
1623
try {
1724
const loadedOptions = loadOptions(request, clerkOptions);
1825

@@ -21,6 +28,9 @@ export function createClerkHandler<TRouter extends AnyRouter>(
2128
acceptsToken: 'any',
2229
});
2330

31+
// Set context for getAuth to use
32+
event.context.auth = () => requestState.toAuth();
33+
2434
const { clerkInitialState, headers } = getResponseClerkState(requestState, loadedOptions);
2535

2636
// Merging the TanStack router context with the Clerk context and loading the router

0 commit comments

Comments
 (0)