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
27 changes: 27 additions & 0 deletions .changeset/ninety-wombats-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
"@clerk/nuxt": patch
"@clerk/tanstack-react-start": patch
---

Allows passing of [`treatPendingAsSignedOut`](https://clerk.com/docs/authentication/configuration/session-tasks#session-handling) to auth functions:

TanStack Start

```ts
const authStateFn = createServerFn({ method: 'GET' }).handler(async () => {
const request = getWebRequest()
const { userId } = await getAuth(request, { treatPendingAsSignedOut: false }) // defaults to true

return { userId }
})
```

Nuxt

```ts
export default eventHandler((event) => {
const { userId } = event.context.auth({ treatPendingAsSignedOut: false }) // defaults to true

return { userId }
})
```
12 changes: 6 additions & 6 deletions packages/nuxt/src/runtime/server/clerkMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { AuthenticateRequestOptions } from '@clerk/backend/internal';
import { AuthStatus, constants, getAuthObjectForAcceptedToken, TokenType } from '@clerk/backend/internal';
import { AuthStatus, constants, getAuthObjectForAcceptedToken } from '@clerk/backend/internal';
import { deprecated } from '@clerk/shared/deprecated';
import { handleNetlifyCacheInDevInstance } from '@clerk/shared/netlifyCacheHandler';
import type { PendingSessionOptions } from '@clerk/types';
import type { EventHandler } from 'h3';
import { createError, eventHandler, setResponseHeader } from 'h3';

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

const authObject = requestState.toAuth();
const authObjectFn = (opts?: PendingSessionOptions) => requestState.toAuth(opts);
const authHandler: AuthFn = ((options?: AuthOptions) => {
const acceptsToken = options?.acceptsToken ?? TokenType.SessionToken;
return getAuthObjectForAcceptedToken({ authObject, acceptsToken });
return getAuthObjectForAcceptedToken({ authObject: authObjectFn(options), acceptsToken: options?.acceptsToken });
}) as AuthFn;

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

event.context.auth = auth;
// Internal serializable state that will be passed to the client
event.context.__clerk_initial_state = createInitialState(authObject);
event.context.__clerk_initial_state = createInitialState(authObjectFn());

await handler?.(event);
});
Expand Down
5 changes: 3 additions & 2 deletions packages/nuxt/src/runtime/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import type {
SessionTokenType,
TokenType,
} from '@clerk/backend/internal';
import type { PendingSessionOptions } from '@clerk/types';

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

/**
* @internal This type is used to define the `auth` function in the event context.
Expand Down Expand Up @@ -41,5 +42,5 @@ export interface AuthFn {
* @example
* const auth = event.context.auth()
*/
(): SessionAuthObject;
(options?: PendingSessionOptions): SessionAuthObject;
}
5 changes: 3 additions & 2 deletions packages/tanstack-react-start/src/server/getAuth.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { AuthenticateRequestOptions, GetAuthFn } from '@clerk/backend/internal';
import { getAuthObjectForAcceptedToken } from '@clerk/backend/internal';
import type { PendingSessionOptions } from '@clerk/types';
import { getContext } from '@tanstack/react-start/server';

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

type GetAuthOptions = { acceptsToken?: AuthenticateRequestOptions['acceptsToken'] };
type GetAuthOptions = PendingSessionOptions & Pick<AuthenticateRequestOptions, 'acceptsToken'>;

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

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

return getAuthObjectForAcceptedToken({ authObject, acceptsToken: opts?.acceptsToken });
}) as GetAuthFn<Request, true>;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AuthStatus, constants } from '@clerk/backend/internal';
import { handleNetlifyCacheInDevInstance } from '@clerk/shared/netlifyCacheHandler';
import type { PendingSessionOptions } from '@clerk/types';
import type { AnyRouter } from '@tanstack/react-router';
import {
type CustomizeStartHandler,
Expand Down Expand Up @@ -30,7 +31,7 @@ export function createClerkHandler<TRouter extends AnyRouter>(
});

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

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