Pseudo Users

Sometimes you do not want to force your application users to sign up with their usernames and passwords. Kii Cloud provides a "pseudo user" feature to accommodate such a need.

Using the pseudo user feature

A pseudo user is a feature to use a Kii Cloud user without a username and password. When a pseudo user is created, an access token is generated. A pseudo user is identified with an access token that is retained on the mobile app, instead of a pair of usernamne and password.

This feature will request a user creation without a username and password to Kii Cloud. When a pseudo user is created, Kii Cloud returns the access token for this user.

When a normal user logs in with its username and password, Kii Cloud issues an access token that indicates the user is logged in. The same effect is achieved when a pseudo user is created.

For a pseudo user, it is essential to keep the access token in the device's local storage until the user no longer needs it. When a user wants to login again, you can do so with the access token by following the steps described in Logging in and Using an Access Token. Please note that if the access token is lost, the user will no longer be able to login as the same user.

A pseudo user can leverage almost all Kii Cloud features like normal users; the only feature not achievable with a pseudo user is getting a new access token. This is because a pseudo user has no associated username and password and thus he cannot login with them so as to get his access token.

A pseudo user can become a normal user by registering his identifier (i.e. username, email address and/or phone number) and password. To allow a pseudo user to access from another device, for example, you might want to first ask the user to become a normal user and then ask him to login as a normal user from another device.

Creating a pseudo user

Here is an example of creating a pseudo user. In this example, the access token issued for the created pseudo user is saved in the device.

  • // Set predefined fields.
    var userFields = {
      "displayName": "Alice",
      "locale": "ja-JP"
    };
    
    // Register a pseudo user.
    KiiUser.registerAsPseudoUser({
      success: function(user) {
        // Save the access token.
        var accessToken = user.getAccessToken();
        $.cookie("access_token", accessToken, { expires: 365 });
      },
      failure: function(user, errorString) {
        // Handle the error.
      }
    }, userFields);
  • // Set predefined fields.
    var userFields = {
      "displayName": "Alice",
      "locale": "ja-JP"
    };
    
    // Register a pseudo user.
    KiiUser.registerAsPseudoUser(null, userFields).then(
      function(user) {
        // Save the access token.
        var accessToken = user.getAccessToken();
        $.cookie("access_token", accessToken, { expires: 365 });
      }
    ).catch(
      function(error) {
        // Handle the error.
        var errorString = error.message;
      }
    );

Here is what is happening in the sample code:

  1. Defines user attributes (optional).
  2. Executes the KiiUser.registerAsPseudoUser method to register the pseudo user.
  3. Executes the user.getAccessToken() method to get the access token issued to the pseudo user. Then saves this token in a Cookie with the jQuery.

When a pseudo user is created, the user will be automatically logged in as the pseudo user; the user can use Kii Cloud just like a normal user.

Please note that a pseudo user will lose a mean to login if their access token is lost (e.g. by the application uninstallation). If you can identify the pseudo user, for example by their display name, you can delete this user on the User Console.

Logging in as a pseudo user

Since a pseudo user has no username or password, you need to log in with the access token, like the following example:

  • // Get an access token.
    string accessToken = $.cookie("access_token");
    
    // Authenticate a user with the access token.
    KiiUser.authenticateWithToken(accessToken, {
      success: function(theUser) {
        // Do something.
      },
      failure: function(theUser, errorString) {
        // Handle the error.
      }
    })
  • // Get an access token.
    string accessToken = $.cookie("access_token");
    
    // Authenticate a user with the access token.
    KiiUser.authenticateWithToken(accessToken).then(
      function(theUser) {
        // Do something.
      }
    ).catch(
      function(error) {
        // Handle the error.
        var theUser = error.target;
        var errorString = error.message;
      }
    );

Changing to a normal user

A pseudo user can be changed to a normal user by setting a username and a password.

  • // If the currently logged-in user is a pseudo user
    if (KiiUser.getCurrentUser().isPseudoUser()) {
      // Get the currently logged-in user.
      var pseudoUser = KiiUser.getCurrentUser();
    
      // Instantiate an IdentityData object.
      var username = "user_123456";
      var emailAddress = "user_123456@example.com";
      var phoneNumber = "+819012345678";
      var password = "123ABC";
      var identityData = {
        "username": username,
        "emailAddress": emailAddress,
        "phoneNumber": phoneNumber
      };
    
      // Set a predefined field and a custom field.
      var userFields = { "displayName":"Player 1",
                         "score": 0
      };
    
      // Remove a custom field.
      var removeFields = ["app_flag"];
    
      // Update the current pseudo user to a normal user.
      pseudoUser.putIdentity(
        identityData,
        "password",
        {
          success: function(theUser) {
            // Do something.
          },
          failure: function(theUser, errorString) {
            // Handle the error.
          }
        },
        userFields,
        removeFields
      );
    }
  • // If the currently logged-in user is a pseudo user
    if (KiiUser.getCurrentUser().isPseudoUser()) {
      // Get the currently logged-in user.
      var pseudoUser = KiiUser.getCurrentUser();
    
      // Instantiate an IdentityData object.
      var username = "user_123456";
      var emailAddress = "user_123456@example.com";
      var phoneNumber = "+819012345678";
      var password = "123ABC";
      var identityData = {
        "username": username,
        "emailAddress": emailAddress,
        "phoneNumber": phoneNumber
      };
    
      // Set a predefined field and a custom field.
      var userFields = { "displayName":"Player 1",
                         "score": 0
      };
    
      // Remove a custom field.
      var removeFields = ["app_flag"];
    
      // Update the current pseudo user to a normal user.
      pseudoUser.putIdentity(identityData, "password", null, userFields, removeFields).then(
        function(theUser) {
          // Do something.
        }
      ).catch(
        function(error) {
          // Handle the error.
          var theUser = error.target;
          var errorString = error.message;
        }
      );
    }

Here is what is happening in the sample code:

  1. Executes the KiiUser.getCurrentUser().isPseudoUser() method to check if the current user is a pseudo user.
  2. Specifies a username, email address and/or phone number.
  3. Specifies predefined and custom fields (optional).
  4. Defines the custom fields to delete in an array (optional).
  5. Executes the KiiUser.putIdentity method with the identityData, userFields, removeFields and password.

With the above steps, a pseudo user becomes a normal user. The user will be able to authenticate and get the access token with the username and password thereafter.

Using Kii Cloud without explicit login

Besides using the pseudo user feature, you can use the following methods to use Kii Cloud without explicit login. Kii recommends using the pseudo user feature because these methods can make the implementation complex or limited.

Creating a user in the mobile app

Your application can automatically create arbitrary username and password, register them to Kii Cloud on behalf of the user and use them every time the user tries to use your mobile app. For example, your mobile app can create a random username and password in background, use them to proceed with the user sign up & sign in, and then store them in the device storage (e.g., SharedPreference on Android and NSUserDefaults on iOS). The user will be able to automatically log in with these username and password until the mobile app is uninstalled or the stored data is explicitly erased.

In this method, your mobile app must assign a unique username to each user.

Once the username and password are erased from the device storage, the user will no longer be able to sign in. Using an immutable value like a device ID for the username and/or password, however, is strongly discouraged so as to prevent the possible privacy infringement.

To provide a support for device migration or back up, simply implement the export and import feature of the username and password stored in the device storage.

Anonymous user

Kii Cloud does support an anonymous user (i.e., a user without any sign in), but the features allowed to the anonymous user are very limited. Most features, like accessing a user-scope data, require the user to sign in.