# Protected Component (/docs/sdks/nextjs/protected)

The `<Protected />` component lets you **conditionally render UI on the client** based on the authentication state and group membership.

Use it for:

- Navigation bars
- Feature flags
- Buttons and actions
- Inline authorization checks

## When to use &lt;Protected /&gt; [#when-to-use-protected-]

- You want to conditionally render UI based on authentication state or group membership
- You don’t want automatic redirects
- You need to perform authorization checks directly within existing UI
- You’re already inside a Client Component

> Do **not** use `<Protected />` to secure sensitive data. For enforcement, use `protectPage()`, `protectApi()`, or `protect()`.

## Before you begin [#before-you-begin]

You should already have:

- A Next.js project
- Environment variables configured in `.env.local`
- Authentication middleware registered

## Basic usage [#basic-usage]

Wrap UI that should only render for authenticated users.

```tsx title="src/components/header.tsx"
"use client";

import { Protected } from "@monocloud/auth-nextjs/components/client";

export function Header() {
  return (
    <header>
      <Protected>
        <span>Welcome back</span>
      </Protected>
    </header>
  );
}
```

**Behavior**

- Renders children if the user is authenticated
- Renders nothing if the user is signed out
- Does not redirect
- Does not throw

## Render fallback content [#render-fallback-content]

Use fallback to render alternate UI when the user is not authenticated.

```tsx title="src/components/header.tsx"
"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>
  );
}
```

## Restrict by group [#restrict-by-group]

You can restrict rendering to users in specific groups.

```tsx title="src/components/admin-only.tsx"
"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**
- Renders children when the user is authenticated **and** belongs to **any of the required groups**.
- Renders nothing when the user is authenticated but does not belong to the required group.
- Renders nothing (or **fallback**, if provided) if the user is **not authenticated**.

## Handle access denied (Group authorization) [#handle-access-denied-group-authorization]

Use `onGroupAccessDenied` to render alternate UI when the user is authenticated but does not belong to the required group.

> The `fallback` prop is only used when the user is not authenticated and is ignored during group authorization failures.

```tsx title="src/components/admin-only.tsx"
'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>
  );
}
```

### Enable group claims [#enable-group-claims]

To use group-based checks:

1. In the [MonoCloud Dashboard](https://dashboard.monocloud.com/), add `groups` to the application scopes
1. Update your `.env.local`:

```bash title=".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.

## Next steps [#next-steps]

Continue securing your application:

- App Router
  - [Protect server components](/docs/sdks/nextjs/app-router/protect-server-components)
  - [Protect client components](/docs/sdks/nextjs/app-router/protect-client-components)
  - [Protect API routes](/docs/sdks/nextjs/app-router/protect-api-routes)
  - [Protect server actions](/docs/sdks/nextjs/app-router/protect-server-actions)

- Pages Router
  - [Protect server rendered pages](/docs/sdks/nextjs/page-router/protect-ssr-page)
  - [Protect client rendered pages](/docs/sdks/nextjs/page-router/protect-csr-page)
  - [Protect API routes](/docs/sdks/nextjs/page-router/protect-api-route)
