From acb9b75edf0a8a1820642daab92d8a88d34566d1 Mon Sep 17 00:00:00 2001 From: Jacek Date: Thu, 10 Oct 2024 18:54:49 -0500 Subject: [PATCH] auth.protect() --- packages/nextjs/src/app-router/server/auth.ts | 27 ++++++++++++++----- .../server/__tests__/clerkMiddleware.test.ts | 3 +-- packages/nextjs/src/server/clerkClient.ts | 4 +-- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/packages/nextjs/src/app-router/server/auth.ts b/packages/nextjs/src/app-router/server/auth.ts index 1df3201e6b..70279323dd 100644 --- a/packages/nextjs/src/app-router/server/auth.ts +++ b/packages/nextjs/src/app-router/server/auth.ts @@ -1,17 +1,16 @@ import type { AuthObject } from '@clerk/backend'; -import type { RedirectFun } from '@clerk/backend/internal'; -import { constants, createClerkRequest, createRedirect } from '@clerk/backend/internal'; +import { constants, createClerkRequest, createRedirect, type RedirectFun } from '@clerk/backend/internal'; import { notFound, redirect } from 'next/navigation'; import { buildClerkProps } from '../../server/buildClerkProps'; import { PUBLISHABLE_KEY, SIGN_IN_URL, SIGN_UP_URL } from '../../server/constants'; import { createGetAuth } from '../../server/createGetAuth'; import { authAuthHeaderMissing } from '../../server/errors'; -import { type AuthProtect, createProtect } from '../../server/protect'; +import { createProtect } from '../../server/protect'; import { decryptClerkRequestData, getAuthKeyFromRequest, getHeader } from '../../server/utils'; import { buildRequestLike } from './utils'; -type Auth = AuthObject & { protect: AuthProtect; redirectToSignIn: RedirectFun> }; +type Auth = AuthObject & { redirectToSignIn: RedirectFun> }; export async function auth(): Promise { require('server-only'); @@ -45,11 +44,25 @@ export async function auth(): Promise { }); }; - const protect = createProtect({ request, authObject, redirectToSignIn, notFound, redirect }); - - return Object.assign(authObject, { protect, redirectToSignIn }); + return Object.assign(authObject, { redirectToSignIn }); } +auth.protect = async (...args: any[]) => { + require('server-only'); + + const request = await buildRequestLike(); + const authObject = await auth(); + + const protect = createProtect({ + request, + authObject, + redirectToSignIn: authObject.redirectToSignIn, + notFound, + redirect, + }); + return protect(...args); +}; + export async function initialState() { return buildClerkProps(await buildRequestLike()); } diff --git a/packages/nextjs/src/server/__tests__/clerkMiddleware.test.ts b/packages/nextjs/src/server/__tests__/clerkMiddleware.test.ts index f7724d9550..5e706b3fb5 100644 --- a/packages/nextjs/src/server/__tests__/clerkMiddleware.test.ts +++ b/packages/nextjs/src/server/__tests__/clerkMiddleware.test.ts @@ -650,8 +650,7 @@ describe('Dev Browser JWT when redirecting to cross origin for page requests', f }); const resp = await clerkMiddleware(async auth => { - const { protect } = await auth(); - protect(); + auth().protect(); })(req, {} as NextFetchEvent); expect(resp?.status).toEqual(307); diff --git a/packages/nextjs/src/server/clerkClient.ts b/packages/nextjs/src/server/clerkClient.ts index 21636e4c46..015a258131 100644 --- a/packages/nextjs/src/server/clerkClient.ts +++ b/packages/nextjs/src/server/clerkClient.ts @@ -75,12 +75,10 @@ interface ClerkClientExport extends ClerkClient { } // TODO SDK-1839 - Remove `clerkClient` singleton in the next major version of `@clerk/nextjs` -const clerkClient = new Proxy(Object.assign(clerkClientForRequest, clerkClientSingleton), { +export const clerkClient = new Proxy(Object.assign(clerkClientForRequest, clerkClientSingleton), { get(target, prop: string, receiver) { deprecated('clerkClient singleton', 'Use `clerkClient()` as a function instead.'); return Reflect.get(target, prop, receiver); }, }) as ClerkClientExport; - -export { clerkClient };