assignRoleToUser()

This function is used for assigning roles to users within a system. Roles define the level of access or permissions that a user has within an application or system. For example, roles could include "ADMIN", "USER", "MANAGER", etc., each granting different levels of privileges.

Here's an example:

import { createBackendFunction, data, utils } from "@magicjs.dev/backend"

export default createBackendFunction(
  async function (name, username, password, otp, hashedOtp) {
    const userDb = data("users")

    const op = await userDb.insertOne({
      name,
      username,
      password: utils.hash(password),
    })
    await utils.assignRoleToUser(String(op.insertedId), "ADMIN")
    return { message: "New user added" }
  },
)

Click here to refer GitHub.

Last updated