Login with the Auto-Saved Credentials

In this approach, your application performs the re-login with the access token and credentials that are automatically stored by the SDK. The access token and credentials are auto-saved by the SDK when a user is logged in or refreshed.

To leverage the feature, specify the android.content.Context instance when initializing Kii Cloud SDK. This will let the SDK auto-save the credentials of the logged in user in the shared preferences. The information that will be auto-saved is as follows:

  • User ID
  • Username
  • Email address
  • Phone number
  • Display name
  • Country
  • Access Token
  • Expiration period of the access token
  • Refresh token

The information is deleted only when the user is logged out.

The following sample code shows how to log in with these credentials.

  • try {
      // Authenticate a user with the stored credentials.
      KiiUser user = KiiUser.loginWithStoredCredentials();
    
      // Refresh the user.
      user.refresh();
    } catch (UserCredentialsNotFoundException e) {
      // Handle the error.
    } catch (AppException e) {
      // Handle the error.
    } catch (IOException e) {
      // Handle the error.
    }
  • // Authenticate a user with the stored credentials.
    KiiUser.loginWithStoredCredentials(new KiiCallback<KiiUser>() {
      @Override
      public void onComplete(KiiUser user, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
    
        // Refresh the user.
        user.refresh(new KiiUserCallBack() {
          @Override
          public void onRefreshCompleted(int token, Exception exception) {
            if (exception != null) {
              // Handle the error.
              return;
            }
          }
        });
      }
    });

You can let a user log in by executing the loginWithStoredCredentials() method if the credentials are stored in the shared preferences. This method just restores the login state from the shared preferences and does not access the server. To get the latest user information from the server, you need to execute the refresh() method.

You can delete the stored credentials from the shared preferences by executing the logOut() method. If the loginWithStoredCredentials() method is called after the logout, a UserCredentialsNotFoundException exception will be thrown.

If the loginWithStoredCredentials() method is used for logging in just after the mobile app is installed, an UserCredentialsNotFoundException exception will be thrown because user credentials do not exist. If an UserCredentialsNotFoundException exception is thrown even the user logs in with the credentials such as the username and password, check if the context information is specified in the initialization code for the Kii Cloud SDK.

Whether the user logs in again or not, the credentials including the access token are saved every time when the user logs in or gets refreshed.

You can also let pseudo users log in with the loginWithStoredCredentials() method by calling the refresh() method of the KiiUser class just after each pseudo user is created.

When you design your mobile app, make sure to implement serialization and screen transition logics. See Restoring the internal state in Kii Balance tutorial for some references.