Login by Manually Specifying an Access Token

In this method, your application retrieves the access token from the SDK and stores it in the application storage. The application can later login with this token when the process is rebooted.

Access token for a user can be retrieved in two ways. One way is by calling the getAccessToken method. Another way is to get an Object by getAccessTokenObject method and get token value from Object by access_token key. In both cases, you have to call the method while the user is logged in. The following sample code shows the both ways:

// Assume that a user has logged in.

// Method #1: Get an access token with the getAccessToken() method.
var accessToken = KiiUser.getCurrentUser().getAccessToken();

// Method #2: Get an access token with the getAccessTokenObject() method.
var tokenObject = KiiUser.getCurrentUser().getAccessTokenObject();
accessToken = tokenObject["access_token"];

// Securely store the access token in the storage with your own function.
storeToken(accessToken);

Here is the sample code for authenticating a user with an access token. By calling the authenticateWithToken method with the access token your user will be authenticated again.

  • // Get an access token from the storage with your own function.
    var accessToken = getStoredToken();
    
    // 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;
      }
    );
  • // Get an access token from the storage with your own function.
    var accessToken = getStoredToken();
    
    // Authenticate a user with the access token.
    KiiUser.authenticateWithToken(accessToken, {
      success: function(theUser) {
        // Do something.
      },
      failure: function(theUser, errorString) {
        // Handle the error.
      }
    })

Please make sure to store an access token in a secure place like Web Storage. It should not be accessible by other applications. If a malicious application gets the access token, it will gain privileges to access Kii Cloud as the owner of this token.

In this approach, your mobile app controls the access token. This allows your mobile app to manage multiple user logins by keeping the access tokens for them and by switching to the appropriate access token. The client SDK can only hold one access token at a time, so the mobile app needs to switch users).