The user to check (currently only the current user is supported)
If null
is passed, this function always returns false
.
The ID of the role to check for
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>
}
Checks whether or not the given user has the role with the given ID