Canceling User Authentication

The unified user authentication framework provides cancel() for terminating the authentication process.

Available APIs

For details about the parameters, return values, and error codes, see cancel.

This topic describes only the API for canceling authentication. For details about the APIs for initiating authentication, see Initiating Authentication and User Authentication.

API Description
cancel(): void Cancels this user authentication.

How to Develop

  1. Check that the application has the ohos.permission.ACCESS_BIOMETRIC permission. For details about how to request permissions, see Requesting Permissions.

  2. Set AuthParam (including the challenge value, UserAuthType, and AuthTrustLevel), obtain a UserAuthInstance instance, and call UserAuthInstance.start to start authentication. For details, see Initiating Authentication.

  3. Call UserAuthInstance.cancel with the UserAuthInstance instance that has initiated the authentication to terminate the authentication process.

Example: Initiate facial and lock screen password authentication at ATL3 or higher and then cancel it.

handleAuthResultAndCanceling(userAuthInstance: userAuth.UserAuthInstance, exampleNumber: number) {
  // ...
    // Start authentication.
    userAuthInstance.start();
    Logger.info('auth start successfully.');
    // ...
      // Cancel the authentication.
      userAuthInstance.cancel();
      Logger.info('auth cancel successfully.');
      // ...
}

/*
 * cancel-authentication.md
 * Initiate facial and lock screen password authentication at ATL3 or higher and then cancel it.
 * */
cancelingUserAuthentication() {
  try {
    const randData = getRandData();
    if (!randData) {
      return;
    }
    // Set authentication parameters.
    const authParam: userAuth.AuthParam = {
      challenge: randData,
      authType: [userAuth.UserAuthType.PIN, userAuth.UserAuthType.FACE, userAuth.UserAuthType.FINGERPRINT],
      authTrustLevel: userAuth.AuthTrustLevel.ATL3,
    };
    // Set the authentication page.
    const widgetParam: userAuth.WidgetParam = {
      title: resourceToString($r('app.string.title')),
    };
    // Obtain an authentication object.
    const userAuthInstance = userAuth.getUserAuthInstance(authParam, widgetParam);
    Logger.info('get userAuth instance successfully.');
    this.handleAuthResultAndCanceling(userAuthInstance, ResultIndex.CANCEL);
  } catch (error) {
    const err: BusinessError = error as BusinessError;
    Logger.error(`auth failed, code is ${err?.code as number}, message is ${err?.message}`);
  }
}

Sample Code