Skip to content

Commit

Permalink
refacter: Updated code examples for Next 15 and to resolve a few mino…
Browse files Browse the repository at this point in the history
…r issues
  • Loading branch information
royanger committed Oct 30, 2024
1 parent 06f2245 commit 6103b1a
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions docs/guides/basic-rbac.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ This guide assumes that you're using Next.js App Router, but the concepts can be
1. Create a `checkRole()` helper that uses the [`auth()`](/docs/references/nextjs/auth) helper to access the user's session claims. From the session claims, access the `publicMetadata` object to check the user's role. The `checkRole()` helper should accept a role of type `Roles`, which you created in the [Create a global TypeScript definition](#create-a-global-typescript-definition) step. It should return `true` if the user has that role or `false` if they do not.

```ts {{ filename: 'utils/roles.ts' }}
import { Roles } from '@/types/global'
import { Roles } from '@/types/globals'
import { auth } from '@clerk/nextjs/server'

export const checkRole = async (role: Roles) => {
const { sessionClaims } = await auth()
return sessionClaims?.metadata.role === role
}
}
```

> [!NOTE]
Expand Down Expand Up @@ -148,12 +148,13 @@ This guide assumes that you're using Next.js App Router, but the concepts can be
1. Use the `checkRole()` function to check if the user has the `admin` role. If they don't, redirect them to the home page.

```tsx {{ filename: 'app/admin/page.tsx' }}
import { auth } from '@clerk/nextjs/server'
import { checkRole } from '@/utils/roles'
import { redirect } from 'next/navigation'

export default function AdminDashboard() {
export default async function AdminDashboard() {
// Protect the page from users who are not admins
if (!checkRole('admin')) {
const isAdmin = await checkRole('admin')
if (!isAdmin) {
redirect('/')
}

Expand All @@ -178,13 +179,15 @@ This guide assumes that you're using Next.js App Router, but the concepts can be
import { clerkClient } from '@clerk/nextjs/server'

export async function setRole(formData: FormData) {
const client = await clerkClient()

// Check that the user trying to set the role is an admin
if (!checkRole('admin')) {
return { message: 'Not Authorized' }
}

try {
const res = await clerkClient().users.updateUser(formData.get('id') as string, {
const res = await clerk.users.updateUser(formData.get('id') as string, {
publicMetadata: { role: formData.get('role') },
})
return { message: res.publicMetadata }
Expand All @@ -195,7 +198,7 @@ This guide assumes that you're using Next.js App Router, but the concepts can be

export async function removeRole(formData: FormData) {
try {
const res = await clerkClient().users.updateUser(formData.get('id') as string, {
const res = await clerk.users.updateUser(formData.get('id') as string, {
publicMetadata: { role: null },
})
return { message: res.publicMetadata }
Expand Down

0 comments on commit 6103b1a

Please sign in to comment.