> ## Documentation Index
> Fetch the complete documentation index at: https://docs.quickblox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Users

> Learn how to manage your users with QuickBlox.

Visit [Key Concepts](/docs/key-concepts) page to learn the most important QuickBlox concepts.

## Before you begin

1. Register a [QuickBlox account](https://admin.quickblox.com/signin). This is a matter of a few minutes and you will be able to use this account to build your apps.
2. Configure QuickBlox SDK for your app. Check out [Setup](/sdks/js-setup) page for more details.
3. Create a user session to be able to use QuickBlox functionality. See [Authentication](/sdks/js-authentication) page to learn how to do it.

## Create user

It's recommended to manage user creation at your backend for production. To learn more you can refer to [QuickBlox API documentation](https://docs.quickblox.com/reference/create-user).

For POCs/MVPs or during development you may want to create users on the fly, you can use `create()` method.

Create a user using the code snippet below. Only login (or email) and password are required. Other fields are optional.

```JavaScript JavaScript theme={null}
var params = {
  login: login,
  password: "someSecret",
  full_name: "QuickBlox Test"
};

QB.users.create(params, function(error, result) {
  if (error) {
    done.fail("Create user error: " + JSON.stringify(error));
  } else {
  }
});
```

<Warning>
  **Security & Privacy**

  It's recommended to [disable permission](/docs/application#set-session-permissions) to create users with application session on **production apps** once user creation is implemented on your backend.

  Email, full name, facebookId and phone number are PII, [configure session permissions](/docs/application#set-session-permissions) according to your privacy requirements.
</Warning>

## Retrieve users

Get a list of users using the `listUsers()` method. The code snippet below shows how to get a list of users created between the two given dates, sorted in ascending order, with `50` users on the page.

```JavaScript JavaScript theme={null}
var params = {
  filter: {
    field: "created_at",
    param: 'between',
    value: '2021-01-01, 2021-05-06'
  },
  order: {
    field: 'created_at',
    sort: 'asc'
  },
  page: 1,
  per_page: 50
};

QB.users.listUsers(params, function(error, result){

});
```

If you want to retrieve only users updated after some specific date time, you can use operators. This is useful if you cache users somehow and do not want to obtain the whole list of your users on every app start. Thus, you can use [search](/sdks/js-users#search-operators) and [sort](/sdks/js-users#sort-operators) operators to list users on the page so that it is easier to see specific users.

If you want to get a paginated list of users from the server, you can set the following fields of the `filter`:

| Field    | Required | Description                                  |
| -------- | -------- | -------------------------------------------- |
| page     | no       | Number of pages with results to be returned. |
| per page | no       | Number of records to return in one page.     |

### Search operators

You can use search operators to get more specific search results. The request below will return users by IDs.

```JavaScript JavaScript theme={null}
var params = {filter: { field: 'id', param: 'in', value: [22,33] }};

QB.users.listUsers(params, function(error, result) {

});
```

Here are the search operators you can use to search for the exact data that you need.

| Search operators | Applicable to types  | Applicable to fields                                                                                               | Description                                |
| ---------------- | -------------------- | ------------------------------------------------------------------------------------------------------------------ | ------------------------------------------ |
| lt               | number, string, date | id, created\_at, updated\_at, last\_request\_at, external\_user\_id, facebook\_id                                  | **Less Than** operator.                    |
| gt               | number, string, date | id, created\_at, updated\_at, last\_request\_at, external\_user\_id, facebook\_id                                  | **Greater Than** operator.                 |
| gte              | number, string, date | id, created\_at, updated\_at, last\_request\_at, external\_user\_id, facebook\_id                                  | **Greater Than** or **Equal** to operator. |
| le               | number, string, date | id, created\_at, updated\_at, last\_request\_at, external\_user\_id, facebook\_id                                  | **Less or Equal to** operator              |
| eq               | number, string, date | id, full\_name, email, login, phone, created\_at, updated\_at, last\_request\_at, external\_user\_id, facebook\_id | **Equal** to operator.                     |
| ne               | number, string, date | id, full\_name, email, login, phone, created\_at, updated\_at, last\_request\_at, external\_user\_id, facebook\_id | **Not Equal** to operator.                 |
| between          | number, string, date | id, created\_at, updated\_at, last\_request\_at, external\_user\_id, facebook\_id                                  | **Contained between values** operator.     |
| in               | number, string, date | id, full\_name, email, login, phone, created\_at, updated\_at, last\_request\_at, external\_user\_id, facebook\_id | **IN** array operator.                     |

### Sort operators

You can use sort operators to order the search results. The request below will return users by the `created_at` field sorted in descending order.

```JavaScript JavaScript theme={null}
var params = {
  filter: {
    field: "created_at",
    param: 'between',
    value: '2021-01-01, 2021-05-06'
  },
  order: {
    field: 'created_at',
    sort: 'desc'
  },
};

QB.users.listUsers(params, function(error, result){

});
```

Here are the sort operators you can use to order the search results.

| Sort operator | Applicable to types | Applicable to fields                                                                                         | Description                                                               |
| ------------- | ------------------- | ------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------- |
| asc           | All types           | id, full\_name, email, login, phone, website, created\_at, updated\_at,last\_request\_at, external\_user\_id | Search results will be sorted in ascending order by the specified field.  |
| desc          | All types           | id, full\_name, email, login, phone, website, created\_at, updated\_at,last\_request\_at, external\_user\_id | Search results will be sorted in descending order by the specified field. |

## Retrieve users by ID

To get a list of users by ID for a current account, use the following code snippet.

```JavaScript JavaScript theme={null}
var searchParams = {filter: { field: 'id', param: 'in', value: [22,33] }};

QB.users.listUsers(searchParams, function(error, result) {

});
```

## Retrieve user by login

To get a list of users by login for a current account, use the following code snippet.

```JavaScript JavaScript theme={null}
var searchParams = {login: "marvin18"};

QB.users.get(searchParams, function(error, user) {

});
```

## Retrieve user by email

To get a list of users by email for a current account, use the following code snippet.

```JavaScript JavaScript theme={null}
var searchParams = {email: "[[email protected]](/cdn-cgi/l/email-protection)"};

QB.users.get(searchParams, function(error, user) {

});
```

## Retrieve users by full name

To get a list of users by full name for a current account, use the following code snippet.
Search **requires** min 3 characters.

```JavaScript JavaScript theme={null}
var searchParams = {
  full_name: "Marvin Samuel", //substring search
  order: {
    field: 'updated_at',
    sort: 'desc'
  },
  page: 1,
  per_page: 50
};

QB.users.get(searchParams, function(error, result) {

});
```

## Retrieve users by phone number

To get a list of users by phone number for a current account, use the following code snippet.

```JavaScript JavaScript theme={null}
var searchParams = {phone: "44678162873"};

QB.users.get(searchParams, function(error, result) {

});
```

## Retrieve user by external user ID

If you have your own database with users (we call these databases as "external databases"), you can use External User ID (`ExternalID` field) in `QBUser` model to link users from QuickBlox with users from your external database.

```JavaScript JavaScript theme={null}
var searchParams = {external_user_id: "675373912"};

QB.users.get(searchParams, function(error, user) {

});
```

## Retrieve users by tags

To get a list of users by tags for a current account, use the following code snippet.

```JavaScript JavaScript theme={null}
var searchParams = {tags: ["apple"]};

QB.users.get(searchParams, function(error, result) {

});
```

## Delete user

A user can delete himself from the platform.

```JavaScript JavaScript theme={null}
var userId = 1;
QB.users.delete(userId, function(error, result) {

});
```

## Reset user password

It's possible to reset a password via email.

```JavaScript JavaScript theme={null}
QB.users.resetPassword("[[email protected]](/cdn-cgi/l/email-protection)", function(error) {

});
```

<Note>
  Make sure to enable the email confirmation. This functionality allows application users to confirm their emails. If a user doesn't confirm the email, the emails won't be sent to this user. As a result, a password reset functionality won't work. To enable the email confirmation, proceed as follows:

  1. Go to the **Dashboard => *YOUR\_APP* => Users => Settings => User registration confirmation** and check the box.
  2. Click the **Save** button.
</Note>

<Warning>
  A password reset functionality is available for the Enterprise plan. [Contact the sales team](https://quickblox.com/enterprise/#get) for more details.
</Warning>

## Update user

Update a user profile by calling the `updateUser()` method. If you want to change your password, you need to provide 2 parameters: `password` and `old_password`. As a result, the updated user entity will be returned.

You can update any other field of the user using the `update()` method. Thus, the snippet below shows how to update a `tag_list` and `custom_data` fields.

```JavaScript JavaScript theme={null}
var userId = 1,
custom_data = JSON.stringify({
  name: 'John',
  age: 31,
  city: 'New York'
}),

updatedUserProfile = {
  tag_list: 'tagOne,tagTwo',
  custom_data
};

QB.users.update(userId, updatedUserProfile, function(error, user) {});
```

| Field        | Required | Description                                                                                                                                                                                                                                                                                     |
| ------------ | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| tag\_list    | no       | User tags. An array of Strings. There are no spaces in the tag format. For example, the "tagOne" format is correct while the "tag one" format is incorrect. The maximum number of tags is 10. If more than 10 tags are provided, an error is returned: tag list should contain maximum 10 tags. |
| custom\_data | no       | User custom data. Should be a String. You can convert any data types to String, or example, JSON, XML, etc.                                                                                                                                                                                     |

## Set user avatar

To set a user avatar, just upload a file to the QuickBlox cloud storage and connect it to the user.

To upload the file to the QuickBlox cloud storage, call the `createAndUpload()` method. Now that the file is uploaded, get the ID of the uploaded file.

To connect the file to the user, set the ID of the uploaded file to the `blob_id` field of the `user` and call the `update()` method. As a result, the user avatar gets updated.

```JavaScript JavaScript theme={null}
// for example, a file from HTML form input field
var inputFile = $("input[type=file]")[0].files[0];

var fileParams = {
  name: inputFile.name,
  file: inputFile,
  type: inputFile.type,
  size: inputFile.size,
  public: false,
};

QB.content.createAndUpload(fileParams, function(error, result) {
  if (error) {
  } else {
    var userId = 1;
    var updatedUserProfile = {
      blob_id: result.id
    };

    QB.users.update(userId, updatedUserProfile, function(error, user) {});
  }
});
```

Pass the following arguments to the `update()` method.

| Argument           | Required | Description                                                                  |
| ------------------ | -------- | ---------------------------------------------------------------------------- |
| userId             | yes      | User ID.                                                                     |
| updatedUserProfile | yes      | Specifies the updatedUserProfile fields that should be set.                  |
| function           | yes      | Specifies a callback function that accepts an error and updated user entity. |

## Get user avatar

Now, other users can get access to your avatar by using the code snippet below. As a result, you will receive a private URL in the response. See [this section](/sdks/js-content#get-file-url) to learn more about file URLs.

```JavaScript JavaScript theme={null}
var fileId = someUser.blob_id;
QB.content.getInfo(fileId, function(error, result) {
  if (error) {
    done.fail("Get file information by ID error: " + JSON.stringify(error));
  } else {
    var fileUID = result.blob.uid;

		var fileUrl = QB.content.privateUrl(fileUID);
		var imageHTML = "<img src='" + fileUrl + "' alt='photo'/>";
  }
});
```
