• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Canceling User Authentication
2
3
4This topic walks you through the process of canceling an authentication in process.
5
6
7## Available APIs
8
9For details about the parameters, return value, and error codes, see [cancel](../../reference/apis-user-authentication-kit/js-apis-useriam-userauth.md#cancel10).
10
11This topic describes only the API for canceling authentication. For details about the APIs for initiating authentication, see [Initiating Authentication](start-authentication.md) and [User Authentication](../../reference/apis-user-authentication-kit/js-apis-useriam-userauth.md).
12
13| API| Description|
14| -------- | -------- |
15| cancel(): void | Cancels this user authentication.|
16
17
18## How to Develop
19
201. Check that the application has the ohos.permission.ACCESS_BIOMETRIC permission. For details about how to request permissions, see [Requesting Permissions](prerequisites.md#requesting-permissions).
21
222. Set [AuthParam](../../reference/apis-user-authentication-kit/js-apis-useriam-userauth.md#authparam10) (including the challenge value, [UserAuthType](../../reference/apis-user-authentication-kit/js-apis-useriam-userauth.md#userauthtype8), and [AuthTrustLevel](../../reference/apis-user-authentication-kit/js-apis-useriam-userauth.md#authtrustlevel8)), obtain a [UserAuthInstance](../../reference/apis-user-authentication-kit/js-apis-useriam-userauth.md#userauthinstance10) instance, and call [UserAuthInstance.start](../../reference/apis-user-authentication-kit/js-apis-useriam-userauth.md#start10) to start authentication.
23   For details, see [Initiating Authentication](start-authentication.md).
24
253. Use [UserAuthInstance.cancel](../../reference/apis-user-authentication-kit/js-apis-useriam-userauth.md#cancel10) with the **UserAuthInstance** instance that has initiated the authentication to cancel the authentication.
26
27Example: Initiate the facial and lock screen password authentication with the authentication trust level greater than or equal to ATL3 and cancel it.
28
29```ts
30import { BusinessError } from  '@kit.BasicServicesKit';
31import { userAuth } from '@kit.UserAuthenticationKit';
32
33const authParam: userAuth.AuthParam = {
34  challenge: new Uint8Array([49, 49, 49, 49, 49, 49]),
35  authType: [userAuth.UserAuthType.PIN, userAuth.UserAuthType.FACE],
36  authTrustLevel: userAuth.AuthTrustLevel.ATL3,
37};
38const widgetParam: userAuth.WidgetParam = {
39  title: 'Verify identity',
40};
41try {
42  // Obtain a UserAuthInstance object.
43  let userAuthInstance = userAuth.getUserAuthInstance(authParam, widgetParam);
44  console.log('get userAuth instance success');
45  // Start user authentication.
46  userAuthInstance.start();
47  console.log('auth start success');
48  // Cancel the authentication.
49  userAuthInstance.cancel();
50  console.log('auth cancel success');
51} catch (error) {
52  const err: BusinessError = error as BusinessError;
53  console.error(`auth catch error. Code is ${err?.code}, message is ${err?.message}`);
54}
55```
56