Skip to content

Commit

Permalink
chore(clerk-react,nextjs): Improve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
panteliselef committed Dec 6, 2023
1 parent 29bb257 commit e07c709
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
6 changes: 4 additions & 2 deletions packages/nextjs/src/app-router/server/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ type AuthSignedIn = AuthObjectWithoutResources<
SignedInAuthObject & {
/**
* @experimental
* This function is experimental as it throws a Nextjs notFound error if user is not authenticated or authorized
* This function is experimental as it throws a Nextjs notFound error if user is not authenticated or authorized.
* In the future we would investigate a way to throw a more appropriate error that clearly describes the not authorized of authenticated status.
*/
protect: (
params?:
Expand All @@ -25,7 +26,8 @@ type AuthSignedOut = AuthObjectWithoutResources<
SignedOutAuthObject & {
/**
* @experimental
* This function is experimental as it throws a Nextjs notFound error if user is not authenticated or authorized
* This function is experimental as it throws a Nextjs notFound error if user is not authenticated or authorized.
* In the future we would investigate a way to throw a more appropriate error that clearly describes the not authorized of authenticated status.
*/
protect: never;
}
Expand Down
9 changes: 5 additions & 4 deletions packages/nextjs/src/app-router/server/controlComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,24 @@ export function SignedOut(props: React.PropsWithChildren) {
type ProtectServerComponentProps = React.ComponentProps<typeof ProtectClientComponent>;

/**
* Use `<Protect/>` in order to prevent unauthenticated or unauthorized user from accessing the children passed to the component.
* Use `<Protect/>` in order to prevent unauthenticated or unauthorized users from accessing the children passed to the component.
*
* Examples:
* ```
* <Protect permission="a_permission_key" />
* <Protect role="a_role_key" />
* <Protect condition={(has) => has({permission:"a_permission_key"})} />
* <Protect condition={(has) => has({role:"a_role_key"})} />
* <Protect fallback={<p>Unauthorized</p>}/>
* <Protect fallback={<p>Unauthorized</p>} />
* ```
*/
export function Protect(props: ProtectServerComponentProps) {
const { children, fallback, ...restAuthorizedParams } = props;
const { has, userId } = auth();

/**
* If neither of the authorization params are passed behave as the `<SignedIn/>`
* If neither of the authorization params are passed behave as the `<SignedIn/>`.
* If fallback is present render that instead of rendering nothing.
*/
if (!restAuthorizedParams.condition && !restAuthorizedParams.role && !restAuthorizedParams.permission) {
if (userId) {
Expand All @@ -58,7 +59,7 @@ export function Protect(props: ProtectServerComponentProps) {
}

/**
* Fallback to custom ui or null if authorization checks failed
* Fallback to UI provided by user or `null` if authorization checks failed
*/
return <>{fallback ?? null}</>;
}
9 changes: 5 additions & 4 deletions packages/react/src/components/controlComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,23 @@ type ProtectProps = React.PropsWithChildren<
>;

/**
* Use `<Protect/>` in order to prevent unauthenticated or unauthorized user from accessing the children passed to the component.
* Use `<Protect/>` in order to prevent unauthenticated or unauthorized users from accessing the children passed to the component.
*
* Examples:
* ```
* <Protect permission="a_permission_key" />
* <Protect role="a_role_key" />
* <Protect condition={(has) => has({permission:"a_permission_key"})} />
* <Protect condition={(has) => has({role:"a_role_key"})} />
* <Protect fallback={<p>Unauthorized</p>}/>
* <Protect fallback={<p>Unauthorized</p>} />
* ```
*/
export const Protect = ({ children, fallback, ...restAuthorizedParams }: ProtectProps) => {
const { has, userId } = useAuth();

/**
* If neither of the authorization params are passed behave as the `<SignedIn/>`
* If neither of the authorization params are passed behave as the `<SignedIn/>`.
* If fallback is present render that instead of rendering nothing.
*/
if (!restAuthorizedParams.condition && !restAuthorizedParams.role && !restAuthorizedParams.permission) {
if (userId) {
Expand All @@ -113,7 +114,7 @@ export const Protect = ({ children, fallback, ...restAuthorizedParams }: Protect
}

/**
* Fallback to custom ui or null if authorization checks failed
* Fallback to UI provided by user or `null` if authorization checks failed
*/
return <>{fallback ?? null}</>;
};
Expand Down

0 comments on commit e07c709

Please sign in to comment.