undefined

Authentication and Users

Every user has to authenticate with ConnectyCube before using any ConnectyCube functionality.

When someone connects with an application using ConnectyCube, the application will need to obtain a session token which provides temporary, secure access to ConnectyCube APIs.

A session token is an opaque string that identifies a user and an application.

Session token rights

There are different types of session tokens to support different use cases:

Session Token Type Description
Application session token This kind of access token is needed to read the app data. Has only READ access to resources
User session token The user token is the most commonly used type of token. This kind of access token is needed any time the app calls an API to read, modify or write a specific user's data on their behalf. Has READ/WRITE access to resources

Create session token

To create an application session use the following code:

ConnectyCube.createSession(function(error, session) {

});

To create a user session use the following code:

var userCredentials = {login: 'cubeuser', password: 'awesomepwd'};

ConnectyCube.createSession(userCredentials, function(error, session) {

});

Upgrade session token (user login)

If you have an application session, you can upgrade it to a user session by calling login method:

var userCredentials = {login: 'cubeuser', password: 'awesomepwd'};
// var userCredentials = {email: 'cubeuser@gmail.com', password: 'awesomepwd'};
// var userCredentials = {provider: 'facebook', keys: {token: 'a876as7db...asg34dasd8wqe'}};

ConnectyCube.login(userCredentials, function(error, user) {

});

Authentication via phone number

Sign In with phone number is supported with (Firebase integration).

You need to create Firebase project_id and obtain Firebase access_token after SMS code verification, then pass these parameters to login method:

var userCredentials = {
  provider: 'firebase_phone',
  firebase_phone[project_id]: "...",
  firebase_phone[access_token]: "...",
};

ConnectyCube.login(userCredentials, function(error, user) {

});

Downgrade session token (user logout)

If you have a user session, you can downgrade it to an application session by calling logout method:

ConnectyCube.logout(function(error) {

});

Session expiration

Expiration time for session token is 2 hours after last request to API. If you perform query with expired token, you will receive the error Required session does not exist. In this case you need to recreate a session token.

There is a special callback function to handle this case:

var CONFIG = {
  on: {
    sessionExpired: function(handleResponse, retry) {

      // call handleResponse() if you do not want to process a session expiration, 
      // so an error will be returned to origin request
      // handleResponse();

      ConnectyCube.createSession(function(error, session) {
        retry(session);
      });
    }
  }
};

ConnectyCube.init(CREDENTIALS, CONFIG);

Destroy session token

To destroy a session use the following code:

ConnectyCube.destroySession(function(error) {

});

User signup

var userProfile = {
       'login': "marvin18",
    'password': "supersecurepwd",
       'email': "awesomeman@gmail.com",
   'full_name': "Marvin Simon",
       'phone': "47802323143",
     'website': "https://dozensofdreams.com",
    'tag_list': "iphone", "apple",
 'custom_data': JSON.stringify({middle_name: "Bartoleo"})
};

ConnectyCube.users.signup(userProfile, function(error, user){

});

Only login (or email) + password are required.

User profile update

var updatedUserProfile = {
       'login': "marvin18",
   'full_name': "Marvin Simon"
};

ConnectyCube.users.update(updatedUserProfile, function(error, user){

});

If you want to change your password, you need to provide 2 parameters: password and old_password. Updated user entity will be returned.

User avatar

You can set a user's avatar. You just need to upload it to the ConnectyCube cloud storage and then connect to user.

// 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};
ConnectyCube.storage.createAndUpload(fileParams, function(error, result) {
  if (!error) {
    var updatedUserProfile = {
      'custom_data': JSON.stringify({'avatar_uid': result.uid})
    };

    ConnectyCube.users.update(updatedUserProfile, function(error, user){

    });
  }
});

Now, other users can get you avatar:

var fileUID = JSON.parse(someUser.custom_data)['avatar_uid'];
var fileUrl = ConnectyCube.storage.privateUrl(fileUID);
var imageHTML = "<img src='" + fileUrl + "' alt='photo'/>";

Password reset

It's possible to reset a password via email:

ConnectyCube.users.resetPassword("awesomeman@gmail.com", function(error){

});

Retrieve users

Retrieve users by ID

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

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

});

Retrieve user by login

var searchParams = {login: "marvin18"};

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

});

Retrieve user by email

var searchParams = {email: "marvin18@example.com"};

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

});

Retrieve users by full name

var searchParams = {full_name: "Marvin Samuel"};

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

});

Retrieve user by phone number

var searchParams = {phone: "44678162873"};

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

});

Retrieve user by external ID

var searchParams = {external_user_id: "675373912"};

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

});

Retrieve users by tags

var searchParams = {tags: ["apple"]};

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

});

Delete user

A user can delete himself from the platform:

ConnectyCube.users.delete(function(error){

});