Sample-users-javascript

Ask tech team
From QuickBlox Developers (API docs, code samples, SDK)
Jump to: navigation, search

Contents

Deprecation notice

Deprecation notice

This documentation is outdated, the newest version of the documentation is now maintained at https://docs.quickblox.com/docs/js-users.

Guide: Getting Started with Users API

User Sign Up

Only login/email and password fields are required, other are optional:

var params = { 'login': "emporio", 'password': "somepass", 'tag': "man"};
 
QB.users.create(params, function(err, user){
  if (user) {
    // success
  } else  {
    // error
  }
});

If registration was successful - you can sign in using you login/email and password.

User Sign In & Social login

There are 2 methods that you can use to sign in to QuickBlox:

  • Signing In with a login & password
  • Signing In with an email & password
  • Signing In using a Facebook/Twitter access token

Sign In with login & password

var params = { 'login': "emporia", 'password': "somepass"};
 
QB.login(params, function(err, user){
  if (user) {
    // success
  } else  {
    // error
  }
});

Sign In with email & password

var params = { 'email': "emporia@gmail.com", 'password': "somepass"};
 
QB.login(params, function(err, user){
  if (user) {
    // success
  } else  {
    // error
  }
});

Sign In using Facebook/Twitter access token

If you want to use your social account to login to QuickBlox, you need to retrieve access token & access the token secret.

For Facebook login you need only token to pass.

var params = { 'provider': "twitter", 'keys[token]': "...", 'keys[secret]': "..."};
 
QB.login(params, function(err, user){
  if (user) {
    // success
  } else  {
    // error
  }
});

Signing In with Firebase phone

You have to start with this guide and obtain Firebase user ID token (access token) https://firebase.google.com/docs/auth/web/phone-auth

Then you can pass it into QuickBlox login method:

var params = {
  provider: 'firebase_phone',
  firebase_phone[project_id]: "...",
  firebase_phone[access_token]: "...",
};
 
// login with twitter_digits params
QB.login(params, function(err, user){
  if (user) {
 
  }else{
 
  }
});

If you are migrating from Twitter Digits - use this guide https://docs.fabric.io/web/digits/web-migration.html

Update user profile

var user_id = 343; // this is your user's id
 
QB.users.update(user_id, {full_name: "samuel jhones", email: "garfin@gmail.com"}, function(err, user){
  if (user) {
     // success  
  } else  {
     // error
  }
});

Attach custom data to user

It's possible to attach custom parameters to the user using custom_data property:

var user_id = 343; // this is your user's id
 
QB.users.update(user_id, {custom_data: JSON.stringify({middle_name: "Carin"});}, function(err, user){
  if (user) {
    // success 
  } else  {
    // error 
  }
});

Update a profile picture (avatar)

To update a user profile picture you must:

  1. Upload new picture to Content module
  2. Connect picture to user through user's blob_id field
QB.content.createAndUpload({file: file, 'public': false}, function(err, blob) {
  if (blob) {
    QB.users.update(self.id, {blob_id: blob.id}, function(err, user) {
      if (user) {
        // success
      } else {
        // error
      }
    });
  } else {
    // error
  }
});

Now other users will be able to see your avatar:

var file_id = user.blob_id;
 
QB.content.getInfo(file_id, function(err, file_info) {
  if (file_info) {
    QB.content.getFile(file_info.uid, function(err, file) {
      if (file) {
        // success 
      } else {
        // error
      }
    });     
  } else {
    // error
  }
});


To delete the user's file just set -1 to blob_id field.

Reset a password

If your forgot your password - you can reset it using the method below. An email with the instruction will be sent to your email address.

QB.users.resetPassword("garmin@gmail.com", function(err, user){
  if (user) {
    // success
  } else  {
    // error
  }
});

Sign Out

QB.logout(function(err, result){
  if (result) {
    // success
  } else {
    // error
  }
});

Retrieve users

There are lots of methods for retrieving users:

  • Retrieve all Users, also with filters
  • Retrieve Users by IDs
  • Retrieve Users by Tags. Tags like groups, in which we can group users
  • Retrieve Users with full name
  • Retrieve User by ID
  • Retrieve User by login
  • Retrieve User by Facebook ID
  • Retrieve User by Twitter ID
  • Retrieve User by email
  • Retrieve User by external ID. You can connect a user from a 3d party system to a QuickBlox user through an external ID.

Retrieve all Users

var params = { page: '1', per_page: '100'};
 
QB.users.listUsers(params, function(err, users){
  if (users) {
    // success
  } else {
    // error
  }
});

Sometimes you need to retrieve all your QuickBlox users. But there is one restriction: you can retrieve a maximum of 100 users per query.

Here is an example how to retrieve all users:

var usersCount = 0;
 
function retrieveAllUsersFromPage(page_number) {
  var params = { page: page_number, per_page: '100'};
  QB.users.listUsers(params, function(err, result){
    if (result) {
      usersCount += result.items.length;
      if(result.total_entries > usersCount){
        retrieveAllUsersFromPage(result.per_page+1);
      }
    } else {
      // error
    }
  });
}
 
// Start
retrieveAllUsersFromPage(1);

It's possible to use some filters and sort parameters:

var params = {filter: { field: 'email', param: 'eq', value: 'nobody@nowhere.org' }};
 
QB.users.listUsers(params, function(err, result){
  if (result) {
    // success
  } else {
    // error
  }
});

Retrieve Users with IDs

var params = {filter: { field: 'id', param: 'in', value: [2,3] }};
 
QB.users.listUsers(params, function(err, result){
  if (result) {
    // success
  } else  {
    // error
  }
});

Retrieve Users with tags

var params = {tags: ["man", "golf"]};
 
QB.users.get(params, function(err, result){
  if (result) {
    // success
  } else  {
    // error
  }
});

Retrieve Users with full names

var params = {full_name: ["Graay Sabs", "Samuel Camy"]};
 
QB.users.get(params, function(err, result){
  if (result) {
    // success
  } else  {
    // error
  }
});

Retrieve User by id

QB.users.get(72, function(err, result){
  if (result) {
    // success
  } else  {
    // error
  }
});

Retrieve User with Facebook ID

var params = {facebook_id: "11227884521232"};
 
QB.users.get(params, function(err, result){
  if (result) {
    // success
  } else  {
    // error
  }
});

Retrieve User with Twitter ID

var params = {twitter_id: "27884521232"};
 
QB.users.get(params, function(err, result){
  if (result) {
    // success
  } else  {
    // error
  }
});

Retrieve User with an external ID

var params = {external: "884521232"};
 
QB.users.get(params, function(err, result){
  if (result) {
    // success
  } else  {
    // error
  }
});

Delete a user

If you need, you can delete a user using methods below:

var user_id = 34;
 
QB.users.delete(user_id, function(err, user){
  if (user) {
    // success
  } else  {
    // error
  }
});
var external_user_id = 34534;
 
QB.users.delete({external: external_user_id}, function(err, user){
  if (user) {
    // success
  } else  {
    // error
  }
});

Integrate QuickBlox with your existing User Base

If you want to integrate QuickBlox, but you have already your User Base, you can use the external_user_id field. Just set it during the QuickBlox user registration:

var params = { 'login': "emporio", 'password': "somepass", 'external_user_id': 438767};
 
QB.users.create(params, function(err, user){
  if (user) {
    // success
  } else  {
    // error
  }
});

Next you can retrieve QuickBlox user by external ID by user ID from your user base and use QuickBlox platform features.