Logging out

Logout a user

In the SDK a user is treated as logged in as long as the user has an access token for the operation to be executed. To logout the user the access token should be removed. This can be done by calling the logout function on the UserClient. The SDK will also send a request to the Token Server to remove the access token to ensure the token is invalidated both on the client as server side. If a refresh token is stored on the device, it will remain after the logout action, so it can be used to login again later.

Example code

Example code to logout a user:

@OnClick(R.id.button_logout)
public void logout() {
  final OneginiClient oneginiClient = OneginiSDK.getOneginiClient(this);
  final UserProfile userProfile = oneginiClient.getUserClient().getAuthenticatedUserProfile();
  OneginiSDK.getOneginiClient(this).getUserClient().logout(
      new OneginiLogoutHandler() {
        @Override
        public void onSuccess() {
          // Go to login screen
          showToast("logoutSuccess");
          startLoginActivity();
        }

        @Override
        public void onError(final OneginiLogoutError oneginiLogoutError) {
          handleLogoutError(oneginiLogoutError, userProfile);
        }
      }
  );
}

private void handleLogoutError(final OneginiLogoutError oneginiLogoutError, final UserProfile userProfile) {
  @OneginiLogoutError.LogoutErrorType final int errorType = oneginiLogoutError.getErrorType();

  if (errorType == OneginiLogoutError.DEVICE_DEREGISTERED) {
    new DeregistrationUtil(this).onDeviceDeregistered();
  } else if (errorType == OneginiLogoutError.USER_DEREGISTERED) {
    new DeregistrationUtil(this).onUserDeregistered(userProfile);
  }

  // other errors don't really require our reaction, but you might consider displaying some message to the user
  showToast("Logout error: " + oneginiLogoutError.getMessage());

  startLoginActivity();
}

Handler methods

  • void onSuccess() is called when the logout was successful, so you should redirect user to a login screen.

  • If void onError(final OneginiLogoutError oneginiLogoutError) was called, it indicates that some error during logout occurred. For example: the SDK was able to remove the access token from storage, but revoke request that was send to the Token Server failed for some reason. In such scenerio you should still redirect user to login screen.

Please note that you can handle errors that are relevant to you differently. To accomplish that you should compare the OneginiLogoutError#getErrorType() value with the OneginiLogoutError error type definitions. The OneginiLogoutError will also contain an additional error description for debugging and possibly a Throwable object which you can get with the getMessage() and getCause() methods.

For more info on error handling see the error handling topic guide.