Deregister user

Deregister the user

Deregistering a user implies the removal of all his data (including access tokens and refresh tokens) from the device. The SDK will also send a request to the Token Server to revoke all tokens. The client credentials will remain stored on the device.

In order to call UserClient#deregisterUser two arguments has to be passed:

  • UserProfile object describing user which you want to deregister,
  • OneginiDeregisterUserProfileHandler handler that will indicate about the result of the deregistration.

Example code

Example code to deregister currently authenticated user:

@OnClick(R.id.button_deregister_user)
public void deregisterUser() {
  final OneginiClient oneginiClient = OneginiSDK.getOneginiClient(this);
  final UserProfile userProfile = oneginiClient.getUserClient().getAuthenticatedUserProfile();
  if (userProfile == null) {
    showToast("userProfile == null");
    return;
  }

  new DeregistrationUtil(this).onUserDeregistered(userProfile);
  oneginiClient.getUserClient().deregisterUser(userProfile, new OneginiDeregisterUserProfileHandler() {
        @Override
        public void onSuccess() {
          onUserDeregistered();
        }

        @Override
        public void onError(final OneginiDeregistrationError oneginiDeregistrationError) {
          onUserDeregistrationError(oneginiDeregistrationError);
        }
      }
  );
}

private void onUserDeregistered() {
  showToast("deregisterUserSuccess");

  startLoginActivity();
}

private void onUserDeregistrationError(final OneginiDeregistrationError oneginiDeregistrationError) {
  @OneginiDeregistrationError.DeregistrationErrorType final int errorType = oneginiDeregistrationError.getErrorType();
  if (errorType == OneginiDeregistrationError.DEVICE_DEREGISTERED) {
    // Deregistration failed due to missing device credentials. Register app once again.
    new DeregistrationUtil(this).onDeviceDeregistered();
  }

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

  startLoginActivity();
}

Please note that any existing user can be deregistered. For more info on error handling see the error handling topic guide.

Handler methods

  • When void onSuccess is called then it's clear that user was successfully deregistered, so you should redirect user to a login screen.

  • Method void onError(final OneginiDeregistrationError oneginiDeregistrationError) is called when some error happened during the action. For example UserProfile was deleted from the device, but request to the server failed. In such situation you would still redirect user to login screen, but you can also make some additional action.

Please note that you can handle differently errors which are relevant to you. To accomplish that you should compare OneginiDeregistrationError#getErrorType() value with OneginiDeregistrationError error type definitions. The OneginiDeregistrationError will also contain additional error description for debugging and possible Exception object which you can get with getErrorDescription() and getException() methods.