diff --git a/docs/guides/basic-rbac.mdx b/docs/guides/basic-rbac.mdx index 497b1fb374..d4204fb15b 100644 --- a/docs/guides/basic-rbac.mdx +++ b/docs/guides/basic-rbac.mdx @@ -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] @@ -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('/') } @@ -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 } @@ -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 }