Skip to content

Commit 083820d

Browse files
authored
chore(tanstack-react-start,nuxt): Pass treatPendingAsSignedOut to auth functions (#6612)
1 parent a23b718 commit 083820d

File tree

5 files changed

+41
-11
lines changed

5 files changed

+41
-11
lines changed

.changeset/ninety-wombats-think.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
"@clerk/nuxt": patch
3+
"@clerk/tanstack-react-start": patch
4+
---
5+
6+
Allows passing of [`treatPendingAsSignedOut`](https://clerk.com/docs/authentication/configuration/session-tasks#session-handling) to auth functions:
7+
8+
TanStack Start
9+
10+
```ts
11+
const authStateFn = createServerFn({ method: 'GET' }).handler(async () => {
12+
const request = getWebRequest()
13+
const { userId } = await getAuth(request, { treatPendingAsSignedOut: false }) // defaults to true
14+
15+
return { userId }
16+
})
17+
```
18+
19+
Nuxt
20+
21+
```ts
22+
export default eventHandler((event) => {
23+
const { userId } = event.context.auth({ treatPendingAsSignedOut: false }) // defaults to true
24+
25+
return { userId }
26+
})
27+
```

packages/nuxt/src/runtime/server/clerkMiddleware.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { AuthenticateRequestOptions } from '@clerk/backend/internal';
2-
import { AuthStatus, constants, getAuthObjectForAcceptedToken, TokenType } from '@clerk/backend/internal';
2+
import { AuthStatus, constants, getAuthObjectForAcceptedToken } from '@clerk/backend/internal';
33
import { deprecated } from '@clerk/shared/deprecated';
44
import { handleNetlifyCacheInDevInstance } from '@clerk/shared/netlifyCacheHandler';
5+
import type { PendingSessionOptions } from '@clerk/types';
56
import type { EventHandler } from 'h3';
67
import { createError, eventHandler, setResponseHeader } from 'h3';
78

@@ -108,10 +109,9 @@ export const clerkMiddleware: ClerkMiddleware = (...args: unknown[]) => {
108109
});
109110
}
110111

111-
const authObject = requestState.toAuth();
112+
const authObjectFn = (opts?: PendingSessionOptions) => requestState.toAuth(opts);
112113
const authHandler: AuthFn = ((options?: AuthOptions) => {
113-
const acceptsToken = options?.acceptsToken ?? TokenType.SessionToken;
114-
return getAuthObjectForAcceptedToken({ authObject, acceptsToken });
114+
return getAuthObjectForAcceptedToken({ authObject: authObjectFn(options), acceptsToken: options?.acceptsToken });
115115
}) as AuthFn;
116116

117117
const auth = new Proxy(authHandler, {
@@ -120,13 +120,13 @@ export const clerkMiddleware: ClerkMiddleware = (...args: unknown[]) => {
120120
// If the property exists on the function, return it
121121
if (prop in target) return Reflect.get(target, prop, receiver);
122122
// Otherwise, get it from the authObject
123-
return authObject?.[prop as keyof typeof authObject];
123+
return authObjectFn()?.[prop as keyof typeof authObjectFn];
124124
},
125125
});
126126

127127
event.context.auth = auth;
128128
// Internal serializable state that will be passed to the client
129-
event.context.__clerk_initial_state = createInitialState(authObject);
129+
event.context.__clerk_initial_state = createInitialState(authObjectFn());
130130

131131
await handler?.(event);
132132
});

packages/nuxt/src/runtime/server/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import type {
66
SessionTokenType,
77
TokenType,
88
} from '@clerk/backend/internal';
9+
import type { PendingSessionOptions } from '@clerk/types';
910

10-
export type AuthOptions = { acceptsToken?: AuthenticateRequestOptions['acceptsToken'] };
11+
export type AuthOptions = PendingSessionOptions & Pick<AuthenticateRequestOptions, 'acceptsToken'>;
1112

1213
/**
1314
* @internal This type is used to define the `auth` function in the event context.
@@ -41,5 +42,5 @@ export interface AuthFn {
4142
* @example
4243
* const auth = event.context.auth()
4344
*/
44-
(): SessionAuthObject;
45+
(options?: PendingSessionOptions): SessionAuthObject;
4546
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import type { AuthenticateRequestOptions, GetAuthFn } from '@clerk/backend/internal';
22
import { getAuthObjectForAcceptedToken } from '@clerk/backend/internal';
3+
import type { PendingSessionOptions } from '@clerk/types';
34
import { getContext } from '@tanstack/react-start/server';
45

56
import { errorThrower } from '../utils';
67
import { clerkHandlerNotConfigured, noFetchFnCtxPassedInGetAuth } from '../utils/errors';
78

8-
type GetAuthOptions = { acceptsToken?: AuthenticateRequestOptions['acceptsToken'] };
9+
type GetAuthOptions = PendingSessionOptions & Pick<AuthenticateRequestOptions, 'acceptsToken'>;
910

1011
export const getAuth: GetAuthFn<Request, true> = (async (request: Request, opts?: GetAuthOptions) => {
1112
if (!request) {
@@ -19,7 +20,7 @@ export const getAuth: GetAuthFn<Request, true> = (async (request: Request, opts?
1920
}
2021

2122
// We're keeping it a promise for now to minimize breaking changes
22-
const authObject = await Promise.resolve(authObjectFn());
23+
const authObject = await Promise.resolve(authObjectFn({ treatPendingAsSignedOut: opts?.treatPendingAsSignedOut }));
2324

2425
return getAuthObjectForAcceptedToken({ authObject, acceptsToken: opts?.acceptsToken });
2526
}) as GetAuthFn<Request, true>;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { AuthStatus, constants } from '@clerk/backend/internal';
22
import { handleNetlifyCacheInDevInstance } from '@clerk/shared/netlifyCacheHandler';
3+
import type { PendingSessionOptions } from '@clerk/types';
34
import type { AnyRouter } from '@tanstack/react-router';
45
import {
56
type CustomizeStartHandler,
@@ -30,7 +31,7 @@ export function createClerkHandler<TRouter extends AnyRouter>(
3031
});
3132

3233
// Set auth object here so it is available immediately in server functions via getAuth()
33-
event.context.auth = () => requestState.toAuth();
34+
event.context.auth = (options?: PendingSessionOptions) => requestState.toAuth(options);
3435

3536
return eventHandler(async ({ request, router, responseHeaders }) => {
3637
const locationHeader = requestState.headers.get(constants.Headers.Location);

0 commit comments

Comments
 (0)