Skip to content

Commit

Permalink
fix(nextjs): Include protect types in auth (#4398)
Browse files Browse the repository at this point in the history
  • Loading branch information
panteliselef authored Oct 24, 2024
1 parent d82886c commit e8ccb55
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/selfish-pianos-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/nextjs": patch
---

Bug fix: Include protect types in `auth`
14 changes: 11 additions & 3 deletions packages/nextjs/src/app-router/server/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ import { notFound, redirect } from 'next/navigation';
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 } from '../../server/protect';
import { createProtect } from '../../server/protect';
import { decryptClerkRequestData, getAuthKeyFromRequest, getHeader } from '../../server/utils';
import { buildRequestLike } from './utils';

type Auth = AuthObject & { redirectToSignIn: RedirectFun<ReturnType<typeof redirect>> };

export async function auth(): Promise<Auth> {
export interface AuthFn {
(): Promise<Auth>;
protect: AuthProtect;
}

export const auth: AuthFn = async () => {
require('server-only');

const request = await buildRequestLike();
Expand Down Expand Up @@ -44,9 +50,9 @@ export async function auth(): Promise<Auth> {
};

return Object.assign(authObject, { redirectToSignIn });
}
};

auth.protect = async (...args: any[]) => {
auth.protect = async (...args) => {
require('server-only');

const request = await buildRequestLike();
Expand All @@ -59,5 +65,7 @@ auth.protect = async (...args: any[]) => {
notFound,
redirect,
});

// @ts-expect-error TS flattens all possible combinations of the for AuthProtect signatures in a union.
return protect(...args);
};

0 comments on commit e8ccb55

Please sign in to comment.