diff --git a/.changeset/real-trainers-knock.md b/.changeset/real-trainers-knock.md new file mode 100644 index 00000000000..64eb2760a31 --- /dev/null +++ b/.changeset/real-trainers-knock.md @@ -0,0 +1,11 @@ +--- +"@clerk/astro": patch +"@clerk/backend": patch +"@clerk/express": patch +"@clerk/fastify": patch +"@clerk/nextjs": patch +"@clerk/nuxt": patch +"@clerk/tanstack-react-start": patch +--- + +Added internal helper type for `auth` and `getAuth()` functions that don't require a request or context parameter diff --git a/.typedoc/__tests__/__snapshots__/file-structure.test.ts.snap b/.typedoc/__tests__/__snapshots__/file-structure.test.ts.snap index e723ceefd8a..5b8327e9a81 100644 --- a/.typedoc/__tests__/__snapshots__/file-structure.test.ts.snap +++ b/.typedoc/__tests__/__snapshots__/file-structure.test.ts.snap @@ -208,6 +208,7 @@ exports[`Typedoc output > should have a deliberate file structure 1`] = ` "nextjs/create-sync-get-auth.mdx", "nextjs/current-user.mdx", "nextjs/get-auth.mdx", + "nextjs/session-auth-with-redirect.mdx", "clerk-react/api-keys.mdx", "clerk-react/checkout-button-props.mdx", "clerk-react/checkout-button.mdx", @@ -247,6 +248,7 @@ exports[`Typedoc output > should have a deliberate file structure 1`] = ` "backend/email-address.mdx", "backend/external-account.mdx", "backend/feature.mdx", + "backend/get-auth-fn-no-request.mdx", "backend/get-auth-fn.mdx", "backend/identification-link.mdx", "backend/infer-auth-object-from-token-array.mdx", diff --git a/packages/astro/src/server/clerk-middleware.ts b/packages/astro/src/server/clerk-middleware.ts index b7e769b20a4..6841e1c40df 100644 --- a/packages/astro/src/server/clerk-middleware.ts +++ b/packages/astro/src/server/clerk-middleware.ts @@ -1,11 +1,10 @@ import type { AuthObject, ClerkClient } from '@clerk/backend'; import type { AuthenticateRequestOptions, + AuthOptions, ClerkRequest, RedirectFun, RequestState, - SignedInAuthObject, - SignedOutAuthObject, } from '@clerk/backend/internal'; import { AuthStatus, @@ -36,7 +35,7 @@ import type { AstroMiddlewareNextParam, AstroMiddlewareReturn, AuthFn, - AuthOptions, + SessionAuthObjectWithRedirect, } from './types'; import { isRedirect, setHeader } from './utils'; @@ -44,10 +43,6 @@ const CONTROL_FLOW_ERROR = { REDIRECT_TO_SIGN_IN: 'CLERK_PROTECT_REDIRECT_TO_SIGN_IN', }; -type ClerkMiddlewareAuthObject = (SignedInAuthObject | SignedOutAuthObject) & { - redirectToSignIn: (opts?: { returnBackUrl?: URL | string | null }) => Response; -}; - type ClerkAstroMiddlewareHandler = ( auth: AuthFn, context: AstroMiddlewareContextParam, @@ -396,7 +391,7 @@ const redirectAdapter = (url: string | URL) => { const createMiddlewareRedirectToSignIn = ( clerkRequest: ClerkRequest, -): ClerkMiddlewareAuthObject['redirectToSignIn'] => { +): SessionAuthObjectWithRedirect['redirectToSignIn'] => { return (opts = {}) => { const err = new Error(CONTROL_FLOW_ERROR.REDIRECT_TO_SIGN_IN) as any; err.returnBackUrl = opts.returnBackUrl === null ? '' : opts.returnBackUrl || clerkRequest.clerkUrl.toString(); diff --git a/packages/astro/src/server/types.ts b/packages/astro/src/server/types.ts index e3c895b15ca..b1236a95eae 100644 --- a/packages/astro/src/server/types.ts +++ b/packages/astro/src/server/types.ts @@ -1,13 +1,5 @@ -import type { AuthObject, InvalidTokenAuthObject, MachineAuthObject, SessionAuthObject } from '@clerk/backend'; -import type { - AuthenticateRequestOptions, - InferAuthObjectFromToken, - InferAuthObjectFromTokenArray, - RedirectFun, - SessionTokenType, - TokenType, -} from '@clerk/backend/internal'; -import type { PendingSessionOptions } from '@clerk/types'; +import type { SessionAuthObject } from '@clerk/backend'; +import type { GetAuthFnNoRequest, RedirectFun } from '@clerk/backend/internal'; import type { APIContext } from 'astro'; /** @@ -26,44 +18,8 @@ export type AstroMiddlewareContextParam = APIContext; export type AstroMiddlewareNextParam = MiddlewareNext; export type AstroMiddlewareReturn = Response | Promise; -export type AuthOptions = PendingSessionOptions & Pick; - -type SessionAuthObjectWithRedirect = SessionAuthObject & { +export type SessionAuthObjectWithRedirect = SessionAuthObject & { redirectToSignIn: RedirectFun; }; -export interface AuthFn { - /** - * @example - * const auth = context.locals.auth({ acceptsToken: ['session_token', 'api_key'] }) - */ - ( - options: AuthOptions & { acceptsToken: T }, - ): - | InferAuthObjectFromTokenArray< - T, - SessionAuthObjectWithRedirect, - MachineAuthObject> - > - | InvalidTokenAuthObject; - - /** - * @example - * const auth = context.locals.auth({ acceptsToken: 'session_token' }) - */ - ( - options: AuthOptions & { acceptsToken: T }, - ): InferAuthObjectFromToken>>; - - /** - * @example - * const auth = context.locals.auth({ acceptsToken: 'any' }) - */ - (options: AuthOptions & { acceptsToken: 'any' }): AuthObject; - - /** - * @example - * const auth = context.locals.auth() - */ - (options?: PendingSessionOptions): SessionAuthObjectWithRedirect; -} +export type AuthFn = GetAuthFnNoRequest; diff --git a/packages/backend/src/internal.ts b/packages/backend/src/internal.ts index 614bd13443e..30c2365150a 100644 --- a/packages/backend/src/internal.ts +++ b/packages/backend/src/internal.ts @@ -13,6 +13,8 @@ export type { InferAuthObjectFromToken, InferAuthObjectFromTokenArray, GetAuthFn, + AuthOptions, + GetAuthFnNoRequest, } from './tokens/types'; export { TokenType } from './tokens/tokenTypes'; diff --git a/packages/backend/src/tokens/__tests__/getAuth.test-d.ts b/packages/backend/src/tokens/__tests__/getAuth.test-d.ts index 5398ed8adf8..3a22688e381 100644 --- a/packages/backend/src/tokens/__tests__/getAuth.test-d.ts +++ b/packages/backend/src/tokens/__tests__/getAuth.test-d.ts @@ -1,49 +1,99 @@ import { expectTypeOf, test } from 'vitest'; +import type { RedirectFun } from '../../createRedirect'; import type { AuthObject, InvalidTokenAuthObject } from '../authObjects'; -import type { GetAuthFn, MachineAuthObject, SessionAuthObject } from '../types'; - -// Across our SDKs, we have a getAuth() function -const getAuth: GetAuthFn = (_request: any, _options: any) => { - return {} as any; -}; - -test('infers the correct AuthObject type for each accepted token type', () => { - const request = new Request('https://example.com'); - - // Session token by default - expectTypeOf(getAuth(request)).toMatchTypeOf(); - - // Individual token types - expectTypeOf(getAuth(request, { acceptsToken: 'session_token' })).toMatchTypeOf(); - expectTypeOf(getAuth(request, { acceptsToken: 'api_key' })).toMatchTypeOf>(); - expectTypeOf(getAuth(request, { acceptsToken: 'm2m_token' })).toMatchTypeOf>(); - expectTypeOf(getAuth(request, { acceptsToken: 'oauth_token' })).toMatchTypeOf>(); - - // Array of token types - expectTypeOf(getAuth(request, { acceptsToken: ['session_token', 'm2m_token'] })).toMatchTypeOf< - SessionAuthObject | MachineAuthObject<'m2m_token'> | InvalidTokenAuthObject - >(); - expectTypeOf(getAuth(request, { acceptsToken: ['m2m_token', 'oauth_token'] })).toMatchTypeOf< - MachineAuthObject<'m2m_token' | 'oauth_token'> | InvalidTokenAuthObject - >(); - - // Any token type - expectTypeOf(getAuth(request, { acceptsToken: 'any' })).toMatchTypeOf(); +import type { GetAuthFn, GetAuthFnNoRequest, MachineAuthObject, SessionAuthObject } from '../types'; + +describe('getAuth() or auth() with request parameter', () => { + const getAuth: GetAuthFn = (_request: any, _options: any) => { + return {} as any; + }; + + test('infers the correct AuthObject type for each accepted token type', () => { + const request = new Request('https://example.com'); + + // Session token by default + expectTypeOf(getAuth(request)).toExtend(); + + // Individual token types + expectTypeOf(getAuth(request, { acceptsToken: 'session_token' })).toExtend(); + expectTypeOf(getAuth(request, { acceptsToken: 'api_key' })).toExtend>(); + expectTypeOf(getAuth(request, { acceptsToken: 'm2m_token' })).toExtend>(); + expectTypeOf(getAuth(request, { acceptsToken: 'oauth_token' })).toExtend>(); + + // Array of token types + expectTypeOf(getAuth(request, { acceptsToken: ['session_token', 'm2m_token'] })).toExtend< + SessionAuthObject | MachineAuthObject<'m2m_token'> | InvalidTokenAuthObject + >(); + expectTypeOf(getAuth(request, { acceptsToken: ['m2m_token', 'oauth_token'] })).toExtend< + MachineAuthObject<'m2m_token' | 'oauth_token'> | InvalidTokenAuthObject + >(); + + // Any token type + expectTypeOf(getAuth(request, { acceptsToken: 'any' })).toExtend(); + }); + + test('verifies discriminated union works correctly with acceptsToken: any', () => { + const request = new Request('https://example.com'); + + const auth = getAuth(request, { acceptsToken: 'any' }); + + if (auth.tokenType === 'session_token') { + expectTypeOf(auth).toExtend(); + } else if (auth.tokenType === 'api_key') { + expectTypeOf(auth).toExtend>(); + } else if (auth.tokenType === 'm2m_token') { + expectTypeOf(auth).toExtend>(); + } else if (auth.tokenType === 'oauth_token') { + expectTypeOf(auth).toExtend>(); + } + }); }); -test('verifies discriminated union works correctly with acceptsToken: any', () => { - const request = new Request('https://example.com'); +describe('getAuth() or auth() without request parameter', () => { + type SessionAuthWithRedirect = SessionAuthObject & { + redirectToSignIn: RedirectFun; + redirectToSignUp: RedirectFun; + }; + + // Mimic Next.js auth() helper + const auth: GetAuthFnNoRequest = (_options: any) => { + return {} as any; + }; + + test('infers the correct AuthObject type for each accepted token type', async () => { + // Session token by default + expectTypeOf(await auth()).toExtend(); + + // Individual token types + expectTypeOf(await auth({ acceptsToken: 'session_token' })).toExtend(); + expectTypeOf(await auth({ acceptsToken: 'api_key' })).toExtend>(); + expectTypeOf(await auth({ acceptsToken: 'm2m_token' })).toExtend>(); + expectTypeOf(await auth({ acceptsToken: 'oauth_token' })).toExtend>(); + + // Array of token types + expectTypeOf(await auth({ acceptsToken: ['session_token', 'm2m_token'] })).toExtend< + SessionAuthWithRedirect | MachineAuthObject<'m2m_token'> | InvalidTokenAuthObject + >(); + expectTypeOf(await auth({ acceptsToken: ['m2m_token', 'oauth_token'] })).toExtend< + MachineAuthObject<'m2m_token' | 'oauth_token'> | InvalidTokenAuthObject + >(); + + // Any token type + expectTypeOf(await auth({ acceptsToken: 'any' })).toExtend(); + }); - const auth = getAuth(request, { acceptsToken: 'any' }); + test('verifies discriminated union works correctly with acceptsToken: any', async () => { + const authObject = await auth({ acceptsToken: 'any' }); - if (auth.tokenType === 'session_token') { - expectTypeOf(auth).toMatchTypeOf(); - } else if (auth.tokenType === 'api_key') { - expectTypeOf(auth).toMatchTypeOf>(); - } else if (auth.tokenType === 'm2m_token') { - expectTypeOf(auth).toMatchTypeOf>(); - } else if (auth.tokenType === 'oauth_token') { - expectTypeOf(auth).toMatchTypeOf>(); - } + if (authObject.tokenType === 'session_token') { + expectTypeOf(authObject).toExtend(); + } else if (authObject.tokenType === 'api_key') { + expectTypeOf(authObject).toExtend>(); + } else if (authObject.tokenType === 'm2m_token') { + expectTypeOf(authObject).toExtend>(); + } else if (authObject.tokenType === 'oauth_token') { + expectTypeOf(authObject).toExtend>(); + } + }); }); diff --git a/packages/backend/src/tokens/types.ts b/packages/backend/src/tokens/types.ts index 1ff799346b5..dab308d6d94 100644 --- a/packages/backend/src/tokens/types.ts +++ b/packages/backend/src/tokens/types.ts @@ -187,14 +187,13 @@ export type MachineAuthObject> = ? AuthenticatedMachineObject | UnauthenticatedMachineObject : never; -type AuthOptions = PendingSessionOptions & { acceptsToken?: AuthenticateRequestOptions['acceptsToken'] }; +export type AuthOptions = PendingSessionOptions & { acceptsToken?: AuthenticateRequestOptions['acceptsToken'] }; type MaybePromise = IsPromise extends true ? Promise : T; /** * Shared generic overload type for getAuth() helpers across SDKs. * - * - Parameterized by the request type (RequestType). * - Handles different accepted token types and their corresponding return types. */ export interface GetAuthFn { @@ -235,3 +234,51 @@ export interface GetAuthFn */ (req: RequestType, options?: PendingSessionOptions): MaybePromise; } + +/** + * Shared generic overload type for auth() or getAuth() helpers that don't require a request parameter. + * + * - Handles different accepted token types and their corresponding return types. + * - The SessionAuthType parameter allows frameworks to extend the base SessionAuthObject with additional properties like redirect methods. + */ +export interface GetAuthFnNoRequest< + SessionAuthType extends SessionAuthObject = SessionAuthObject, + ReturnsPromise extends boolean = false, +> { + /** + * @example + * const authObject = await auth({ acceptsToken: ['session_token', 'api_key'] }) + */ + ( + options: AuthOptions & { acceptsToken: T }, + ): MaybePromise< + | InferAuthObjectFromTokenArray>> + | InvalidTokenAuthObject, + ReturnsPromise + >; + + /** + * @example + * const authObject = await auth({ acceptsToken: 'session_token' }) + */ + ( + options: AuthOptions & { acceptsToken: T }, + ): MaybePromise< + InferAuthObjectFromToken>>, + ReturnsPromise + >; + + /** + * @example + * const authObject = await auth({ acceptsToken: 'any' }) + */ + ( + options: AuthOptions & { acceptsToken: 'any' }, + ): MaybePromise | SessionAuthType, ReturnsPromise>; + + /** + * @example + * const authObject = await auth() + */ + (options?: PendingSessionOptions): MaybePromise; +} diff --git a/packages/express/src/getAuth.ts b/packages/express/src/getAuth.ts index 7b2dfdba9a6..0119cbeec7f 100644 --- a/packages/express/src/getAuth.ts +++ b/packages/express/src/getAuth.ts @@ -1,13 +1,10 @@ -import type { AuthenticateRequestOptions, GetAuthFn } from '@clerk/backend/internal'; +import type { AuthOptions, GetAuthFn } from '@clerk/backend/internal'; import { getAuthObjectForAcceptedToken } from '@clerk/backend/internal'; -import type { PendingSessionOptions } from '@clerk/types'; import type { Request as ExpressRequest } from 'express'; import { middlewareRequired } from './errors'; import { requestHasAuthObject } from './utils'; -type GetAuthOptions = PendingSessionOptions & { acceptsToken?: AuthenticateRequestOptions['acceptsToken'] }; - /** * Retrieves the Clerk AuthObject using the current request object. * @@ -15,7 +12,7 @@ type GetAuthOptions = PendingSessionOptions & { acceptsToken?: AuthenticateReque * @returns {AuthObject} Object with information about the request state and claims. * @throws {Error} `clerkMiddleware` or `requireAuth` is required to be set in the middleware chain before this util is used. */ -export const getAuth: GetAuthFn = ((req: ExpressRequest, options?: GetAuthOptions) => { +export const getAuth: GetAuthFn = ((req: ExpressRequest, options?: AuthOptions) => { if (!requestHasAuthObject(req)) { throw new Error(middlewareRequired('getAuth')); } diff --git a/packages/fastify/src/getAuth.ts b/packages/fastify/src/getAuth.ts index 7d56567845c..b5fae25cafb 100644 --- a/packages/fastify/src/getAuth.ts +++ b/packages/fastify/src/getAuth.ts @@ -1,17 +1,10 @@ -import type { - AuthenticateRequestOptions, - GetAuthFn, - SignedInAuthObject, - SignedOutAuthObject, -} from '@clerk/backend/internal'; +import type { AuthOptions, GetAuthFn, SignedInAuthObject, SignedOutAuthObject } from '@clerk/backend/internal'; import { getAuthObjectForAcceptedToken } from '@clerk/backend/internal'; import type { FastifyRequest } from 'fastify'; import { pluginRegistrationRequired } from './errors'; -type GetAuthOptions = { acceptsToken?: AuthenticateRequestOptions['acceptsToken'] }; - -export const getAuth: GetAuthFn = ((req: FastifyRequest, options?: GetAuthOptions) => { +export const getAuth: GetAuthFn = ((req: FastifyRequest, options?: AuthOptions) => { const authReq = req as FastifyRequest & { auth: SignedInAuthObject | SignedOutAuthObject }; if (!authReq.auth) { diff --git a/packages/nextjs/src/app-router/server/auth.ts b/packages/nextjs/src/app-router/server/auth.ts index 27defb1bfd4..3a4f0efd544 100644 --- a/packages/nextjs/src/app-router/server/auth.ts +++ b/packages/nextjs/src/app-router/server/auth.ts @@ -1,13 +1,6 @@ -import type { AuthObject, InvalidTokenAuthObject, MachineAuthObject, SessionAuthObject } from '@clerk/backend'; -import type { - AuthenticateRequestOptions, - InferAuthObjectFromToken, - InferAuthObjectFromTokenArray, - RedirectFun, - SessionTokenType, -} from '@clerk/backend/internal'; +import type { SessionAuthObject } from '@clerk/backend'; +import type { AuthOptions, GetAuthFnNoRequest, RedirectFun } from '@clerk/backend/internal'; import { constants, createClerkRequest, createRedirect, TokenType } from '@clerk/backend/internal'; -import type { PendingSessionOptions } from '@clerk/types'; import { notFound, redirect } from 'next/navigation'; import { PUBLISHABLE_KEY, SIGN_IN_URL, SIGN_UP_URL } from '../../server/constants'; @@ -24,7 +17,7 @@ import { buildRequestLike } from './utils'; /** * `Auth` object of the currently active user and the `redirectToSignIn()` method. */ -type SessionAuthWithRedirect = SessionAuthObject & { +export type SessionAuthWithRedirect = SessionAuthObject & { /** * The `auth()` helper returns the `redirectToSignIn()` method, which you can use to redirect the user to the sign-in page. * @@ -33,7 +26,7 @@ type SessionAuthWithRedirect = SessionAuthObject & { * > [!NOTE] * > `auth()` on the server-side can only access redirect URLs defined via [environment variables](https://clerk.com/docs/guides/development/clerk-environment-variables#sign-in-and-sign-up-redirects) or [`clerkMiddleware` dynamic keys](https://clerk.com/docs/reference/nextjs/clerk-middleware#dynamic-keys). */ - redirectToSignIn: RedirectFun; + redirectToSignIn: RedirectFun>; /** * The `auth()` helper returns the `redirectToSignUp()` method, which you can use to redirect the user to the sign-up page. @@ -43,49 +36,10 @@ type SessionAuthWithRedirect = SessionAuthObject & { * > [!NOTE] * > `auth()` on the server-side can only access redirect URLs defined via [environment variables](https://clerk.com/docs/guides/development/clerk-environment-variables#sign-in-and-sign-up-redirects) or [`clerkMiddleware` dynamic keys](https://clerk.com/docs/reference/nextjs/clerk-middleware#dynamic-keys). */ - redirectToSignUp: RedirectFun; + redirectToSignUp: RedirectFun>; }; -export type AuthOptions = PendingSessionOptions & { acceptsToken?: AuthenticateRequestOptions['acceptsToken'] }; - -export interface AuthFn> { - /** - * @example - * const authObject = await auth({ acceptsToken: ['session_token', 'api_key'] }) - */ - ( - options: AuthOptions & { acceptsToken: T }, - ): Promise< - | InferAuthObjectFromTokenArray< - T, - SessionAuthWithRedirect, - MachineAuthObject> - > - | InvalidTokenAuthObject - >; - - /** - * @example - * const authObject = await auth({ acceptsToken: 'session_token' }) - */ - ( - options: AuthOptions & { acceptsToken: T }, - ): Promise< - InferAuthObjectFromToken, MachineAuthObject>> - >; - - /** - * @example - * const authObject = await auth({ acceptsToken: 'any' }) - */ - (options: AuthOptions & { acceptsToken: 'any' }): Promise; - - /** - * @example - * const authObject = await auth() - */ - (options?: PendingSessionOptions): Promise>; - +export type AuthFn = GetAuthFnNoRequest & { /** * `auth` includes a single property, the `protect()` method, which you can use in two ways: * - to check if a user is authenticated (signed in) @@ -105,7 +59,7 @@ export interface AuthFn> { * `auth.protect()` can be used to check if a user is authenticated or authorized to access certain parts of your application or even entire routes. See detailed examples in the [dedicated guide](https://clerk.com/docs/organizations/verify-user-permissions). */ protect: AuthProtect; -} +}; /** * The `auth()` helper returns the [`Auth`](https://clerk.com/docs/reference/backend/types/auth-object) object of the currently active user, as well as the [`redirectToSignIn()`](https://clerk.com/docs/reference/nextjs/app-router/auth#redirect-to-sign-in) method. diff --git a/packages/nextjs/src/server/clerkMiddleware.ts b/packages/nextjs/src/server/clerkMiddleware.ts index 3bdf51da911..971a8705130 100644 --- a/packages/nextjs/src/server/clerkMiddleware.ts +++ b/packages/nextjs/src/server/clerkMiddleware.ts @@ -69,7 +69,7 @@ export type ClerkMiddlewareSessionAuthObject = (SignedInAuthObject | SignedOutAu */ export type ClerkMiddlewareAuthObject = ClerkMiddlewareSessionAuthObject; -export type ClerkMiddlewareAuth = AuthFn; +export type ClerkMiddlewareAuth = AuthFn; type ClerkMiddlewareHandler = ( auth: ClerkMiddlewareAuth, diff --git a/packages/nuxt/src/runtime/server/__tests__/auth.test-d.ts b/packages/nuxt/src/runtime/server/__tests__/auth.test-d.ts index d2a97f285a0..0d7f0cdca48 100644 --- a/packages/nuxt/src/runtime/server/__tests__/auth.test-d.ts +++ b/packages/nuxt/src/runtime/server/__tests__/auth.test-d.ts @@ -6,49 +6,49 @@ import type { AuthFn } from '../types'; test('infers the correct AuthObject type for each accepted token type', () => { // Mock event object const event = { - locals: { + context: { auth: (() => {}) as AuthFn, }, }; // Session token by default - expectTypeOf(event.locals.auth()).toMatchTypeOf(); + expectTypeOf(event.context.auth()).toExtend(); // Individual token types - expectTypeOf(event.locals.auth({ acceptsToken: 'session_token' })).toMatchTypeOf(); - expectTypeOf(event.locals.auth({ acceptsToken: 'api_key' })).toMatchTypeOf>(); - expectTypeOf(event.locals.auth({ acceptsToken: 'm2m_token' })).toMatchTypeOf>(); - expectTypeOf(event.locals.auth({ acceptsToken: 'oauth_token' })).toMatchTypeOf>(); + expectTypeOf(event.context.auth({ acceptsToken: 'session_token' })).toExtend(); + expectTypeOf(event.context.auth({ acceptsToken: 'api_key' })).toExtend>(); + expectTypeOf(event.context.auth({ acceptsToken: 'm2m_token' })).toExtend>(); + expectTypeOf(event.context.auth({ acceptsToken: 'oauth_token' })).toExtend>(); // Array of token types - expectTypeOf(event.locals.auth({ acceptsToken: ['session_token', 'm2m_token'] })).toMatchTypeOf< + expectTypeOf(event.context.auth({ acceptsToken: ['session_token', 'm2m_token'] })).toExtend< SessionAuthObject | MachineAuthObject<'m2m_token'> | InvalidTokenAuthObject >(); - expectTypeOf(event.locals.auth({ acceptsToken: ['m2m_token', 'oauth_token'] })).toMatchTypeOf< + expectTypeOf(event.context.auth({ acceptsToken: ['m2m_token', 'oauth_token'] })).toExtend< MachineAuthObject<'m2m_token' | 'oauth_token'> | InvalidTokenAuthObject >(); // Any token type - expectTypeOf(event.locals.auth({ acceptsToken: 'any' })).toMatchTypeOf(); + expectTypeOf(event.context.auth({ acceptsToken: 'any' })).toExtend(); }); test('verifies discriminated union works correctly with acceptsToken: any', () => { // Mock event object const event = { - locals: { + context: { auth: (() => {}) as AuthFn, }, }; - const auth = event.locals.auth({ acceptsToken: 'any' }); + const auth = event.context.auth({ acceptsToken: 'any' }); if (auth.tokenType === 'session_token') { - expectTypeOf(auth).toMatchTypeOf(); + expectTypeOf(auth).toExtend(); } else if (auth.tokenType === 'api_key') { - expectTypeOf(auth).toMatchTypeOf>(); + expectTypeOf(auth).toExtend>(); } else if (auth.tokenType === 'm2m_token') { - expectTypeOf(auth).toMatchTypeOf>(); + expectTypeOf(auth).toExtend>(); } else if (auth.tokenType === 'oauth_token') { - expectTypeOf(auth).toMatchTypeOf>(); + expectTypeOf(auth).toExtend>(); } }); diff --git a/packages/nuxt/src/runtime/server/types.ts b/packages/nuxt/src/runtime/server/types.ts index 796e772a45e..d851b65a667 100644 --- a/packages/nuxt/src/runtime/server/types.ts +++ b/packages/nuxt/src/runtime/server/types.ts @@ -1,11 +1,4 @@ -import type { AuthObject, InvalidTokenAuthObject, MachineAuthObject, SessionAuthObject } from '@clerk/backend'; -import type { - AuthenticateRequestOptions, - InferAuthObjectFromToken, - InferAuthObjectFromTokenArray, - SessionTokenType, - TokenType, -} from '@clerk/backend/internal'; +import type { AuthenticateRequestOptions, GetAuthFnNoRequest } from '@clerk/backend/internal'; import type { PendingSessionOptions } from '@clerk/types'; export type AuthOptions = PendingSessionOptions & Pick; @@ -13,34 +6,4 @@ export type AuthOptions = PendingSessionOptions & Pick( - options: AuthOptions & { acceptsToken: T }, - ): - | InferAuthObjectFromTokenArray>> - | InvalidTokenAuthObject; - - /** - * @example - * const auth = event.context.auth({ acceptsToken: 'session_token' }) - */ - ( - options: AuthOptions & { acceptsToken: T }, - ): InferAuthObjectFromToken>>; - - /** - * @example - * const auth = event.context.auth({ acceptsToken: 'any' }) - */ - (options: AuthOptions & { acceptsToken: 'any' }): AuthObject; - - /** - * @example - * const auth = event.context.auth() - */ - (options?: PendingSessionOptions): SessionAuthObject; -} +export type AuthFn = GetAuthFnNoRequest; diff --git a/packages/tanstack-react-start/src/server/getAuth.ts b/packages/tanstack-react-start/src/server/getAuth.ts index be95e3c9435..9001e588fd1 100644 --- a/packages/tanstack-react-start/src/server/getAuth.ts +++ b/packages/tanstack-react-start/src/server/getAuth.ts @@ -1,14 +1,11 @@ -import type { AuthenticateRequestOptions, GetAuthFn } from '@clerk/backend/internal'; +import type { AuthOptions, 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 = PendingSessionOptions & Pick; - -export const getAuth: GetAuthFn = (async (request: Request, opts?: GetAuthOptions) => { +export const getAuth: GetAuthFn = (async (request: Request, opts?: AuthOptions) => { if (!request) { return errorThrower.throw(noFetchFnCtxPassedInGetAuth); }