Skip to Content
Clerk logo

Clerk Docs

Ctrl + K
Go to clerkstage.dev

<Protect>

The <Protect> component is used for authorization. It only renders its children when the current user has the specified permission or role in the organization.

Usage

You can pass either a permission or role key to <Protect> to limit who can see the content it renders. The recommended approach is to use permission because this lets you modify Roles without breaking your application. Permissions can be assigned to different Roles with ease.

If you do not pass either prop, <Protect> behaves the same as <SignedIn> and will render its children if the user is signed in, regardless of their Role or its Permissions.

For more complex authorization logic, pass conditional logic to the condition prop.

Render content by Permissions

The children of the following component will only be visible to users with Roles that have the org:invoices:create permission.

import { Protect } from "@clerk/nextjs"; export default function ProtectPage() { return ( <Protect permission="org:invoices:create" fallback={<p>You do not have the permissions to create an invoice.</p>} > {children} </Protect> ); }

Render content by Role

While authorization by permission is recommended, <Protect/> allows a role key to be passed for convenience. The children of the following component will only be visible to users with the org:billing Role.

import { Protect } from "@clerk/nextjs"; export default function ProtectPage() { return ( <Protect role="org:billing" fallback={<p>Only a member of the Billing department can access this content.</p>} > {children} </Protect> ); }

Properties

NameTypeDescription
conditionhas => booleanOptional conditional logic that renders the children if it returns true.
fallbackJSXAn optional snippet of JSX to show when a user doesn't have the role or permission to access the protected content.
permissionstringOptional string corresponding to a Role's Permission in the format org:<resource>:<action>
rolestringOptional string corresponding to an Organization's Role in the format org:<role>

<Protect> can only accept permission or role, not both. permission is the recommended approach.

What did you think of this content?

Clerk © 2023