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.getMessage());

  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.

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

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