Sanity Library Reference Docs
    Preparing search index...

    Function userHasRole

    • Checks whether or not the given user has the role with the given ID

      Parameters

      • user: Omit<CurrentUser, "role"> & { role?: string }

        The user to check (currently only the current user is supported) If null is passed, this function always returns false.

      • roleId: string

        The ID of the role to check for

      Returns boolean

      true if the user has the role, false otherwise

      Fetch the current user and check if they have the role "administrator":

      import {userHasRole, useCurrentUser} from 'sanity'

      export function MyComponent() {
      const user = useCurrentUser()
      const hasAdminRole = userHasRole(user, 'administrator')
      return <div>Is administrator: {hasAdminRole ? 'Yes' : 'No'}</div>
      }