# 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:

```tsx
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.](https://github.com/skyslit/magicjs.dev/blob/main/packages/backend/src/utils.ts#L28)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://skyslit.gitbook.io/magicjs/api-references/backend/utils/assignroletouser.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
