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

  1. 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.
  2. Configure QuickBlox SDK for your app. Check out our Setup page for more details.
  3. Create a user session to be able to use QuickBlox functionality. See our Authentication page to learn how to do it.

Create user

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

String login = "chrispeterson";
String password = "superPassword";
String email = "[email protected]";
int blobId = 34253543;
int externalUserId = 2398734;
int facebookId = 2928734;
int twitterId = 92374392;
String fullName = "John Smith";
String phone = "+380772342381";
String webSite = "www.moo.com";
String customData = "";
String tagList = "";

try {
  QBUser? user = await QB.users.createUser(login, password, email, blobId: blobId, externalUserId: externalUserId, facebookId: facebookId, twitterId: twitterId, fullName: fullName, phone: phone, website: website, customData: customData, tagList: tagList);
} on PlatformException catch (e) {
  // Some error occurred, look at the exception message for more details
}
ArgumentRequiredDescription
loginyesUser login.
passwordyesUser password.
emailnoUser email.
blobIdnoUser blob ID.
externalUserIdnoUser external ID.
facebookIdnoUser facebook ID.
twitterIdnoUser twitter ID.
fullNamenoUser full name.
phonenoUser phone.
websitenoUser website.
customDatanoUser custom data.
tagListnoUser tag list.

Retrieve users

Get a list of users using the getUsers() method. The code snippet below shows how to get a list of users created between the two given dates, sorted in descending order, and limited to 100 users on the page.

try {
  QBFilter filter = QBFilter();
  filter.type = QBUsersFilterTypes.DATE;
  filter.field = QBUsersFilterFields.CREATED_AT;
  filter.operator = QBUsersFilterOperators.BETWEEN;
  filter.value = "2021-08-01T10:00:00Z, 2021-08-12T10:00:00Z";

  QBSort sort = QBSort();
  sort.field = QBUsersSortFields.CREATED_AT;
  sort.type = QBUsersSortTypes.DATE;
  sort.ascending = false;
  
  int page = 5;
  int perPage = 100;

  List<QBUser?> userList = await QB.users.getUsers(filter: filter, sort: sort, page: page, perPage: perPage);
} on PlatformException catch (e) {
    // Some error occurred, look at the exception message for more details
}

The method getUsers() accepts two (optional) arguments of the object type that can have the following fields:

FieldRequired
filternoSpecifies filtering criteria for the field.
sortnoSpecifies sorting criteria for the field.
pagenoNumber of pages with results to be returned.
perPagenoNumber of records to return in one page.

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.

Search operators

You can use search operators to get more specific search results. The request below will return a list of users with a login other than the one specified.

try {
  QBFilter filter = QBFilter();
  filter.type = QBUsersFilterTypes.STRING;
  filter.field = QBUsersFilterFields.LOGIN;
  filter.operator = QBUsersFilterOperators.NE;
  filter.value = "admin";

  List<QBUser?> userList = await QB.users.getUsers(filter: filter);
} on PlatformException catch (e) {
    // Some error occurred, look at the exception message for more details
}

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

Search operatorsApplicable to typesApplicable to fieldsDescription
ltnumber, string, dateid, created_at, updated_at, last_request_at, external_user_id, facebook_idLess Than operator.
gtnumber, string, dateid, created_at, updated_at, last_request_at, external_user_id, facebook_idGreater Than operator.
genumber, string, dateid, created_at, updated_at, last_request_at, external_user_id, facebook_idGreater Than or Equal to operator.
lenumber, string, dateid, created_at, updated_at, last_request_at, external_user_id, facebook_idLess or Equal to operator
eqnumber, string, dateid, full_name, email, login, phone, created_at, updated_at, last_request_at, external_user_id, facebook_idEqual to operator.
nenumber, string, dateid, full_name, email, login, phone, created_at, updated_at, last_request_at, external_user_id, facebook_idNot Equal to operator.
betweennumber, string, dateid, created_at, updated_at, last_request_at, external_user_id, facebook_idContained between values operator.
innumber, string, dateid, full_name, email, login, phone, created_at, updated_at, last_request_at, external_user_id, facebook_idIN array operator.

Sort operators

You can use sort operators to order the search results. The request below will return a list of users with the login sorted in descending order.

try {
  QBSort sort = QBSort();
  sort.field = QBUsersSortFields.LOGIN;
  sort.type = QBUsersSortTypes.STRING;
  sort.ascending = false;

  List<QBUser?> userList = await QB.users.getUsers(sort: sort);
} on PlatformException catch (e) {
    // Some error occurred, look at the exception message for more details
}

Here are the sort options that you can use to order the search results:

Sort optionsApplicable to typesApplicable to fieldsDescription
ascendingAll typesid, full_name, email, login, phone, website, created_at, updated_at,
last_request_at, external_user_id
Sort results in ascending order by setting the ascending as true.
descendingAll typesid, full_name, email, login, phone, website, created_at, updated_at,
last_request_at, external_user_id
Sort results in descending order by setting the ascending as false.

Retrieve users by tag

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

List<String> tags = ["test"];
int page = 5;
int perPage = 100;

try {
  List<QBUser?> userList = await QB.users.getUsersByTag(tags, page: page, perPage: perPage
  );
} on PlatformException catch (e) {
  // Some error occurred, look at the exception message for more details
}
ArgumentRequiredDescription
tagsyesAn array of tags. Min: 1.
pagenoNumber of pages with results to be returned. Default: 1.
perPagenoNumber of records to return in one page. Min: 1. Default: 100.

Reset user password

📘

This feature is coming soon. Please check back later. For more updates and questions, feel free to contact our Customer Support Team.

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 newPassword. The updated user entity will be returned. You can also update any other field of the user using the updateUser() method. Thus, the snippet below shows how to update a tagList and customData fields.

String login = "chrispeterson";
String customData = "";
String tagList = "tagOne, tagTwo";

try {
  var someObject = {};
  someObject["name"] = "John Smith";
  someObject["age"] = "31";
  someObject["city"] = "New York";
    
  String customData = json.encode(someObject);
  
  QBUser? user = await QB.users.updateUser(login, customData: customData, tagList: tagList);
} on PlatformException catch (e) {
  // Some error occurred, look at the exception message for more details
}
ArgumentRequiredDescription
loginyesUser login.
customDatanoUser custom data. Should be a String. You can convert any data types to String, for example, JSON, XML , etc.
tagListnoUser tags. A comma separated String without any spaces. A tag should contain alphanumeric characters and start with a letter. For example, the "tagOne" format is correct while the "tag one" format is incorrect.

If more that 10 tags are provided, an error is returned: tag list should contain maximum 10 tags.

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 upload() method and pass the imageFileUrl to it. The imageFileUrl is a path to the file in the device filesystem. 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 blobId field of the user and call the updateUser() method. As a result, the user avatar gets updated.

String imageFileUrl = "content://data/image.jpg";
String login = "chrispeterson";
int blobId = 324987234;

try {
  QBFile? file = await QB.content.upload(imageFileUrl);
  if (file != null){
    int blobId = file!.id;
    QBUser? updatedUser = QB.users.updateUser(login, blobId: blobId);
  }
} on PlatformException catch (e) {
  // Some error occurred, look at the exception message for more details
}

Pass the following arguments to the updateUser() method:

ArgumentRequiredDescription
userLoginyesUser login.
blobIdnoUser blob ID.

Get user avatar

Now, other users can get access to your avatar by calling the getPrivateURL() method. As a result, you will receive a private URL in the response. See this section to learn more about file URLs.

// get access to the avatar that was set in user's "blobId" property
int avatarFileId = user.blobId;

try {
  QBFile? file = await QB.content.getInfo(avatarFileId);
  if (file != null){
    String uid  = file!.uid;
    String? fileUrl = await QB.content.getPrivateURL(uid);
  }
} on PlatformException catch (e) {
  // Some error occurred, look at the exception message for more details
}

What’s Next