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
3 changes: 1 addition & 2 deletions packages/nextjs/src/app-router/server/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const auth: AuthFn = async () => {
return Object.assign(authObject, { redirectToSignIn });
};

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

const request = await buildRequestLike();
Expand All @@ -66,6 +66,5 @@ auth.protect = async (...args) => {
redirect,
});

// @ts-expect-error TS flattens all possible combinations of the for AuthProtect signatures in a union.
return protect(...args);
};
4 changes: 4 additions & 0 deletions packages/nextjs/src/server/__tests__/clerkMiddleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ describe('ClerkMiddleware type tests', () => {
clerkMiddlewareMock(async (auth, _event, _request) => {
// @ts-expect-error - system permissions are not allowed
(await auth()).has({ permission: 'org:sys_foo' });
// @ts-expect-error - system permissions are not allowed
await auth.protect(has => has({ permission: 'org:sys_foo' }));
// @ts-expect-error - system permissions are not allowed
await auth.protect({ permission: 'org:sys_foo' });
});
});

Expand Down
10 changes: 8 additions & 2 deletions packages/nextjs/src/server/protect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import type { AuthObject } from '@clerk/backend';
import type { RedirectFun, SignedInAuthObject } from '@clerk/backend/internal';
import { constants } from '@clerk/backend/internal';
import type {
CheckAuthorizationFromSessionClaims,
CheckAuthorizationParamsFromSessionClaims,
CheckAuthorizationParamsWithCustomPermissions,
CheckAuthorizationWithCustomPermissions,
OrganizationCustomPermissionKey,
} from '@clerk/types';

import { constants as nextConstants } from '../constants';
Expand All @@ -15,10 +18,13 @@ type AuthProtectOptions = { unauthorizedUrl?: string; unauthenticatedUrl?: strin
* Throws a Nextjs notFound error if user is not authenticated or authorized.
*/
export interface AuthProtect {
(params?: CheckAuthorizationParamsWithCustomPermissions, options?: AuthProtectOptions): Promise<SignedInAuthObject>;
<P extends OrganizationCustomPermissionKey>(
params?: CheckAuthorizationParamsFromSessionClaims<P>,
options?: AuthProtectOptions,
): Promise<SignedInAuthObject>;

(
params?: (has: CheckAuthorizationWithCustomPermissions) => boolean,
params?: (has: CheckAuthorizationFromSessionClaims) => boolean,
options?: AuthProtectOptions,
): Promise<SignedInAuthObject>;

Expand Down
40 changes: 21 additions & 19 deletions packages/types/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,6 @@ type DisallowSystemPermissions<P extends string> = P extends `${OrganizationSyst
? 'System permissions are not included in session claims and cannot be used on the server-side'
: P;

/**
* Type guard for server-side authorization checks using session claims.
* System permissions are not allowed since they are not included
* in session claims and cannot be verified on the server side.
*/
export type CheckAuthorizationFromSessionClaims = <P extends OrganizationCustomPermissionKey>(
isAuthorizedParams: WithReverification<
| {
role: OrganizationCustomRoleKey;
permission?: never;
}
| {
role?: never;
permission: DisallowSystemPermissions<P>;
}
| { role?: never; permission?: never }
>,
) => boolean;

export type CheckAuthorizationFn<Params> = (isAuthorizedParams: Params) => boolean;

export type CheckAuthorizationWithCustomPermissions =
Expand Down Expand Up @@ -87,6 +68,27 @@ type CheckAuthorizationParams = WithReverification<
}
>;

/**
* Type guard for server-side authorization checks using session claims.
* System permissions are not allowed since they are not included
* in session claims and cannot be verified on the server side.
*/
export type CheckAuthorizationFromSessionClaims = <P extends OrganizationCustomPermissionKey>(
isAuthorizedParams: CheckAuthorizationParamsFromSessionClaims<P>,
) => boolean;

export type CheckAuthorizationParamsFromSessionClaims<P extends OrganizationCustomPermissionKey> = WithReverification<
| {
role: OrganizationCustomRoleKey;
permission?: never;
}
| {
role?: never;
permission: DisallowSystemPermissions<P>;
}
| { role?: never; permission?: never }
>;
Comment on lines +76 to +90
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I simply restructured it, no actual changes were made


export interface SessionResource extends ClerkResource {
id: string;
status: SessionStatus;
Expand Down
Loading