Users
Learn how to manage your users with QuickBlox.
Visit our Key Concepts page to get an overall understanding of the most important QuickBlox concepts.
Before you begin
- Register a QuickBlox account. This is a matter of a few minutes and you will be able to use this account to build your apps.
- Configure QuickBlox SDK for your app. Check out our Setup page for more details.
- Create a user session to be able to use QuickBlox functionality. See our 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.
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.
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 {
}
});
Security & Privacy
It's recommended to disable permission 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 according to your privacy requirements.
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.
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 and sort 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.
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.
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.
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.
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.
var searchParams = {email: "[email protected]"};
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.
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.
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.
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.
var searchParams = {tags: ["apple"]};
QB.users.get(searchParams, function(error, result) {
});
Delete user
A user can delete himself from the platform.
var userId = 1;
QB.users.delete(userId, function(error, result) {
});
Reset user password
It's possible to reset a password via email.
QB.users.resetPassword("[email protected]", function(error) {
});
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:
- Go to the Dashboard => YOUR_APP => Users => Settings => User registration confirmation and check the box.
- Click the Save button.
A password reset functionality is available for the Enterprise plan. Contact the sales team for more details.
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.
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.
// 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 to learn more about file URLs.
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'/>";
}
});
Updated over 1 year ago