Skip to content

Commit

Permalink
auth.protect()
Browse files Browse the repository at this point in the history
  • Loading branch information
jacekradko committed Oct 10, 2024
1 parent 42e8e5c commit acb9b75
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
27 changes: 20 additions & 7 deletions packages/nextjs/src/app-router/server/auth.ts
Original file line number Diff line number Diff line change
@@ -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<ReturnType<typeof redirect>> };
type Auth = AuthObject & { redirectToSignIn: RedirectFun<ReturnType<typeof redirect>> };

export async function auth(): Promise<Auth> {
require('server-only');
Expand Down Expand Up @@ -45,11 +44,25 @@ export async function auth(): Promise<Auth> {
});
};

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());
}
3 changes: 1 addition & 2 deletions packages/nextjs/src/server/__tests__/clerkMiddleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions packages/nextjs/src/server/clerkClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

0 comments on commit acb9b75

Please sign in to comment.