The <Protected /> component lets you conditionally render UI on the client based on the authentication state and group membership.
Use it for:
Do not use<Protected />to secure sensitive data. For enforcement, useprotectPage(),protectApi(), orprotect().
You should already have:
.env.localWrap UI that should only render for authenticated users.
"use client";
import { Protected } from "@monocloud/auth-nextjs/components/client";
export function Header() {
return (
<header>
<Protected>
<span>Welcome back</span>
</Protected>
</header>
);
}
Behavior
Use fallback to render alternate UI when the user is not authenticated.
"use client";
import { Protected } from "@monocloud/auth-nextjs/components/client";
export function Header() {
return (
<Protected fallback={<a href="/signin">Sign in</a>}>
<a href="/dashboard">Dashboard</a>
</Protected>
);
}
You can restrict rendering to users in specific groups.
"use client";
import { Protected } from "@monocloud/auth-nextjs/components/client";
export function AdminOnly() {
return (
<Protected groups={["admin"]}>
<button>Delete user</button>
</Protected>
);
}
How it works
Use onGroupAccessDenied to render alternate UI when the user is authenticated but does not belong to the required group.
Thefallbackprop is only used when the user is not authenticated and is ignored during group authorization failures.
'use client';
import { Protected } from '@monocloud/auth-nextjs/components/client';
export function AdminPanel() {
return (
<Protected
groups={['admin']}
onGroupAccessDenied={user => (
<p>User {user.email}, does not have access.</p>
)}
>
<section>Admin settings</section>
</Protected>
);
}
To use group-based checks:
groups to the application scopes.env.local:MONOCLOUD_AUTH_SCOPES=openid profile email groups
After updating scopes, users must sign out and sign back in for the new claims to be included in their session.
Continue securing your application: