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.

  • try {
      // Set predefined fields and a custom field.
      UserFields userFields = new UserFields();
      userFields.putDisplayName("Player 1");
      userFields.putLocale(new LocaleContainer(Locale.getDefault()));
      userFields.set("HighScore", 0);
    
      // Register a pseudo user.
      KiiUser pseudoUser = KiiUser.registerAsPseudoUser(userFields);
    
      // Save the access token.
      String accessToken = pseudoUser.getAccessToken();
    
      // Store the access token in the storage with your own function.
      storeToken(accessToken);
    } catch (IOException e) {
      // Handle the error.
    } catch (AppException e) {
      // Handle the error.
    }
  • // Set predefined fields and a custom field.
    UserFields userFields = new UserFields();
    userFields.putDisplayName("Player 1");
    userFields.putLocale(new LocaleContainer(Locale.getDefault()));
    userFields.set("HighScore", 0);
    
    // Register a pseudo user.
    KiiUser.registerAsPseudoUser(userFields, new KiiUserRegisterCallback() {
      @Override
      public void onRegisterCompleted(KiiUser kiiUser, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
        // Save the access token.
        String accessToken = kiiUser.getAccessToken();
    
        // Store the access token in the storage with your own function.
        storeToken(accessToken);
      }
    });

Here is what is happening in the sample code:

  1. Defines a user attribute (optional).
  2. Executes the registerAsPseudoUser method to create and register a pseudo user.
  3. Executes the KiiUser.getAccessToken method to get the access token issued to the pseudo user. Then saves this token in the device.

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 from the storage with your own function.
    String accessToken = getStoredToken();
    
    // Authenticate a user with the access token.
    try {
      KiiUser.loginWithToken(accessToken);
    } catch (IOException e) {
      // Handle the error.
    } catch (AppException e) {
      // Handle the error.
    }
  • // Get an access token from the storage with your own function.
    String accessToken = getStoredToken();
    
    // Authenticate a user with the access token.
    KiiUser.loginWithToken(new KiiUserCallBack() {
      @Override
      public void onLoginCompleted(int token, KiiUser user, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
      }
    }, accessToken);

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.
      KiiUser pseudoUser = KiiUser.getCurrentUser();
    
      try {
        // Instantiate an IdentityData object.
        IdentityData.Builder builder =
          IdentityData.Builder.newWithName("user_123456");
        builder.withEmail("user_123456@example.com")
          .withPhone("+819012345678");
        IdentityData identityData = builder.build();
    
        // Set a predefined field and a custom field.
        UserFields userFields = new UserFields();
        userFields.putDisplayName("Player 1");
        userFields.set("HighScore", 0);
    
        // Remove a custom field.
        userFields.removeFromServer("app_flag");
    
        // Update the current pseudo user to a normal user.
        pseudoUser.putIdentity(identityData, userFields, "123ABC");
      } catch (AlreadyHasIdentityException e) {
        // The current user already has identity information. You can use this user as a named user.
      } catch (IOException e) {
        // Handle the error.
      } catch (AppException e) {
        // Handle the error.
      }
    }
  • // If the currently logged-in user is a pseudo user
    if (KiiUser.getCurrentUser().isPseudoUser()) {
      // Get the currently logged-in user.
      KiiUser pseudoUser = KiiUser.getCurrentUser();
    
      // Instantiate an IdentityData object.
      IdentityData.Builder builder =
        IdentityData.Builder.newWithName("user_123456");
      builder.withEmail("user_123456@example.com")
        .withPhone("+819012345678");
      IdentityData identityData = builder.build();
    
      // Set a predefined field and a custom field.
      UserFields userFields = new UserFields();
      userFields.putDisplayName("Player 1");
      userFields.set("HighScore", 0);
    
      // Remove a custom field.
      userFields.removeFromServer("app_flag");
    
      // Update the current pseudo user to a normal user.
      pseudoUser.putIdentity(identityData, userFields, "123ABC",new KiiUserPutIdentityCallback() {
        @Override
        public void onPutIdentityCompleted(KiiUser kiiUser, Exception exception) {
          if (exception != null) {
            // Handle the error.
            return;
          }
        }
      });
    }

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. Creates an IdentityData.Builder instance by specifying the username.
  3. Specifies an email address and phone number in the IdentityData.Builder instance (optional).
  4. Executes the IdentityData.Builder.build method to create an IdentityData instance.
  5. Creates a UserFields instance and put the user attributes (optional).
  6. Executes the KiiUser.putIdentity method with the IdentityData, 'UserFields` 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 from that point forward.

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.