Skip to content

Commit

Permalink
Replace example with auth in a layout
Browse files Browse the repository at this point in the history
  • Loading branch information
panteliselef committed Oct 24, 2024
1 parent e1081c6 commit 0d17b6c
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions docs/references/nextjs/auth.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,21 @@ export async function GET() {

## Use `auth.protect()` to check if a user is authenticated

`auth.protect()` can be used in a `layout.tsx` file to protect the entire route, including all children.
`auth.protect()` can be used in a `page.tsx` file to protect any page in your application.

In the following example,

- the `auth.protect()` helper is used to check if a user visiting any `/dashboard` route is authenticated.
- the `auth.protect()` helper is used to check if a user visiting the `/dashboard` route is authenticated.
- If the user is not authenticated, they will be redirected to the sign-in route.
- If the user is authenticated, they can view any `/dashboard` route and its children.
- If the user is authenticated, they can view the `/dashboard` page.

```tsx {{ filename: 'app/dashboard/layout.tsx' }}
```tsx {{ filename: 'app/dashboard/page.tsx' }}
import { auth } from '@clerk/nextjs/server'

export default async function Layout({ children }: { children: React.ReactNode }) {
await auth.protect()
export default async function Page() {
const { userId } = await auth.protect()

return <>{children}</>
return <h1>Welcome, {userId}</h1>
}
```

Expand Down

0 comments on commit 0d17b6c

Please sign in to comment.