March 16, 2025
+ 12

Requiring Sync Gateway role

Related Topics:    access()   |   channel()   |   expiry()   |   requireAccess()   |   requireAdmin()   |   requireRole()   |   requireUser()   |   role()   |   throw()

Function

requireRole(rolename)

Purpose

Use the requireRole() function to reject document updates that are not made by user with the specified role or roles, as shown in Example 1.

Arguments

Argument Description

rolename

Must be a string identifying a role, or an array of strings identifying multiple roles; the function is applied to each role in the array.

If the value resolves to null the function result is a no-op.

Note — Role names must always be prefixed with role:; an exception is thrown if a role name doesn’t conform with this rule..

Context

The function requires that the user has at least one of the specified roles. If that is not the case it signals rejection by throwing an exception. The rest of the sync function will not be run.

Use

Example 1. requireRole(rolename)
javascript
requireRole("admin"); (1) requireRole(["admin", "old-timer"]); (2)
1 Throw an error unless the user has the "admin" role:
2 Throw an error unless the user has one or more of those roles: