createRoleManager
createRoleManager(
d1,options?):object
Defined in: packages/auth/src/roles.ts:68
Creates a role manager backed by a Cloudflare D1 database.
Provides methods to query, assign, replace, and remove user roles.
When roleGrants is configured, assignment methods enforce authorization
rules so that, for example, an editor cannot promote someone to admin.
Parameters
Section titled “Parameters”D1Database
The Cloudflare D1 database binding.
options?
Section titled “options?”RoleManagerOptions
Optional configuration for table name and role grant rules.
Returns
Section titled “Returns”object
An object with getRoles, setRole, setRoles, and removeRole methods.
getRoles()
Section titled “getRoles()”getRoles(
userId):Promise<string[]>
Parameters
Section titled “Parameters”userId
Section titled “userId”string
Returns
Section titled “Returns”Promise<string[]>
removeRole()
Section titled “removeRole()”removeRole(
userId,role):Promise<void>
Parameters
Section titled “Parameters”userId
Section titled “userId”string
string
Returns
Section titled “Returns”Promise<void>
setRole()
Section titled “setRole()”setRole(
userId,role,caller?):Promise<void>
Parameters
Section titled “Parameters”userId
Section titled “userId”string
string
caller?
Section titled “caller?”CallerOptions
Returns
Section titled “Returns”Promise<void>
setRoles()
Section titled “setRoles()”setRoles(
userId,roles,caller?):Promise<void>
Parameters
Section titled “Parameters”userId
Section titled “userId”string
string[]
caller?
Section titled “caller?”CallerOptions
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”import { createRoleManager } from "@cfast/auth";
const roles = createRoleManager(env.DB, { roleGrants: { admin: ["admin", "editor", "user"], editor: ["user"], },});
const userRoles = await roles.getRoles(userId);await roles.setRole(userId, "editor", { callerRoles: ["admin"] });