harmony 鸿蒙Canceling User Authentication

  • 2025-06-12
  • 浏览 (4)

Canceling User Authentication

Use cancel() to terminate the authentication process when needed.

Available APIs

For details about the parameters, return value, 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.

import { BusinessError } from  '@kit.BasicServicesKit';
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { userAuth } from '@kit.UserAuthenticationKit';

try {
  const rand = cryptoFramework.createRandom();
  const len: number = 16;
  const randData: Uint8Array = rand?.generateRandomSync(len)?.data;
  // Set authentication parameters.
  const authParam: userAuth.AuthParam = {
    challenge: randData,
    authType: [userAuth.UserAuthType.PIN, userAuth.UserAuthType.FACE],
    authTrustLevel: userAuth.AuthTrustLevel.ATL3,
  };
  // Set the authentication page.
  const widgetParam: userAuth.WidgetParam = {
    title: 'Verify identity',
  };
  // Obtain a UserAuthInstance object.
  const userAuthInstance = userAuth.getUserAuthInstance(authParam, widgetParam);
  console.info('get userAuth instance success');
  // Start user authentication.
  userAuthInstance.start();
  console.info('auth start success');
  // Cancel the authentication.
  userAuthInstance.cancel();
  console.info('auth cancel success');
} catch (error) {
  const err: BusinessError = error as BusinessError;
  console.error(`auth catch error. Code is ${err?.code}, message is ${err?.message}`);
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙User Authentication Kit (User Authentication Service)

harmony 鸿蒙Applying Custom Authentication

harmony 鸿蒙Obtaining Enrolled Credential Status

harmony 鸿蒙Obtaining Supported Authentication Capabilities

harmony 鸿蒙Before You Start

harmony 鸿蒙Initiating User Authentication

harmony 鸿蒙Using userAuthIcon

harmony 鸿蒙Introduction to User Authentication Kit

0  赞