Skip to content

Commit 41803e4

Browse files
committed
chore(nextjs): Minor improvements
1 parent 1a6f3c5 commit 41803e4

File tree

5 files changed

+34
-8
lines changed

5 files changed

+34
-8
lines changed

packages/backend/src/tokens/authObjects.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ const createHasAuthorization =
210210
}): CheckAuthorizationWithCustomPermissions =>
211211
params => {
212212
if (!params?.permission && !params?.role) {
213-
throw new Error('Missing params. `has` from `useAuth` requires a permission or role key to be passed.');
213+
throw new Error(
214+
'Missing parameters. `has` from `auth` or `getAuth` requires a permission or role key to be passed.',
215+
);
214216
}
215217

216218
if (!orgId || !userId || !orgRole || !orgPermissions) {

packages/nextjs/src/app-router/server/controlComponents.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,27 @@ export function SignedOut(props: React.PropsWithChildren) {
1717

1818
type ProtectServerComponentProps = React.ComponentProps<typeof ProtectClientComponent>;
1919

20+
/**
21+
* Use `<Protect/>` in order to prevent unauthenticated or unauthorized user from accessing the children passed to the component.
22+
*
23+
* Examples:
24+
* ```
25+
* <Protect permission="a_permission_key" />
26+
* <Protect role="a_role_key" />
27+
* <Protect condition={(has) => has({permission:"a_permission_key"})} />
28+
* <Protect condition={(has) => has({role:"a_role_key"})} />
29+
* <Protect fallback={<p>Unauthorized</p>}/>
30+
* ```
31+
*/
2032
export function Protect(props: ProtectServerComponentProps) {
2133
const { children, fallback, ...restAuthorizedParams } = props;
22-
const { has, userId, sessionId } = auth();
34+
const { has, userId } = auth();
2335

2436
/**
2537
* If neither of the authorization params are passed behave as the `<SignedIn/>`
2638
*/
2739
if (!restAuthorizedParams.condition && !restAuthorizedParams.role && !restAuthorizedParams.permission) {
28-
if (userId && sessionId) {
40+
if (userId) {
2941
return <>{children}</>;
3042
}
3143
return <>{fallback ?? null}</>;

packages/react/src/components/controlComponents.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,26 @@ type ProtectProps = React.PropsWithChildren<
7373
}
7474
>;
7575

76+
/**
77+
* Use `<Protect/>` in order to prevent unauthenticated or unauthorized user from accessing the children passed to the component.
78+
*
79+
* Examples:
80+
* ```
81+
* <Protect permission="a_permission_key" />
82+
* <Protect role="a_role_key" />
83+
* <Protect condition={(has) => has({permission:"a_permission_key"})} />
84+
* <Protect condition={(has) => has({role:"a_role_key"})} />
85+
* <Protect fallback={<p>Unauthorized</p>}/>
86+
* ```
87+
*/
7688
export const Protect = ({ children, fallback, ...restAuthorizedParams }: ProtectProps) => {
77-
const { has, userId, sessionId } = useAuth();
89+
const { has, userId } = useAuth();
7890

7991
/**
8092
* If neither of the authorization params are passed behave as the `<SignedIn/>`
8193
*/
8294
if (!restAuthorizedParams.condition && !restAuthorizedParams.role && !restAuthorizedParams.permission) {
83-
if (userId && sessionId) {
95+
if (userId) {
8496
return <>{children}</>;
8597
}
8698
return <>{fallback ?? null}</>;

packages/react/src/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ export const customLinkWrongProps = (componentName: string) =>
4545
`Missing props. <${componentName}.Link /> component requires the following props: url, label and labelIcon.`;
4646

4747
export const useAuthHasRequiresRoleOrPermission =
48-
'Missing params. `has` from `useAuth` requires a permission or role key to be passed.';
48+
'Missing parameters. `has` from `useAuth` requires a permission or role key to be passed.';

packages/types/src/organizationMembership.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export type OrganizationCustomRoleKey = string;
4141

4242
/**
4343
* @deprecated This type is deprecated and will be removed in the next major release.
44-
* Use `OrganizationCustomRole` instead.
44+
* Use `OrganizationCustomRoleKey` instead.
4545
* MembershipRole includes `admin`, `basic_member`, `guest_member`. With the introduction of "Custom roles"
4646
* these types will no longer match a developer's custom logic.
4747
*/
@@ -58,7 +58,7 @@ export type OrganizationSystemPermissionKey =
5858
| 'org:sys_domains:read';
5959

6060
/**
61-
* OrganizationPermission is a combination of system and custom permissions.
61+
* OrganizationPermissionKey is a combination of system and custom permissions.
6262
* System permissions are only accessible from FAPI and client-side operations/utils
6363
*/
6464
export type OrganizationPermissionKey = Autocomplete<OrganizationSystemPermissionKey>;

0 commit comments

Comments
 (0)