1# Canceling User Authentication 2 3<!--Kit: User Authentication Kit--> 4<!--Subsystem: UserIAM--> 5<!--Owner: @WALL_EYE--> 6<!--SE: @lichangting518--> 7<!--TSE: @jane_lz--> 8 9Use **cancel()** to terminate the authentication process when needed. 10 11## Available APIs 12 13For details about the parameters, return value, and error codes, see [cancel](../../reference/apis-user-authentication-kit/js-apis-useriam-userauth.md#cancel10). 14 15This 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). 16 17| API| Description| 18| -------- | -------- | 19| cancel(): void | Cancels this user authentication.| 20 21## How to Develop 22 231. 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). 24 252. 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. For details, see [Initiating Authentication](start-authentication.md). 26 273. Call [UserAuthInstance.cancel](../../reference/apis-user-authentication-kit/js-apis-useriam-userauth.md#cancel10) with the **UserAuthInstance** instance that has initiated the authentication to terminate the authentication process. 28 29Example: Initiate facial and lock screen password authentication at ATL3 or higher and then cancel it. 30 31```ts 32import { BusinessError } from '@kit.BasicServicesKit'; 33import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 34import { userAuth } from '@kit.UserAuthenticationKit'; 35 36try { 37 const rand = cryptoFramework.createRandom(); 38 const len: number = 16; 39 let randData: Uint8Array | null = null; 40 let retryCount = 0; 41 while(retryCount < 3){ 42 randData = rand?.generateRandomSync(len)?.data; 43 if(randData){ 44 break; 45 } 46 retryCount++; 47 } 48 if(!randData){ 49 return; 50 } 51 // Set authentication parameters. 52 const authParam: userAuth.AuthParam = { 53 challenge: randData, 54 authType: [userAuth.UserAuthType.PIN, userAuth.UserAuthType.FACE], 55 authTrustLevel: userAuth.AuthTrustLevel.ATL3, 56 }; 57 // Set the authentication page. 58 const widgetParam: userAuth.WidgetParam = { 59 title: 'Verify identity', 60 }; 61 // Obtain an authentication object. 62 const userAuthInstance = userAuth.getUserAuthInstance(authParam, widgetParam); 63 console.info('get userAuth instance success'); 64 // Start user authentication. 65 userAuthInstance.start(); 66 console.info('auth start success'); 67 // Cancel the authentication. 68 userAuthInstance.cancel(); 69 console.info('auth cancel success'); 70} catch (error) { 71 const err: BusinessError = error as BusinessError; 72 console.error(`auth catch error. Code is ${err?.code}, message is ${err?.message}`); 73} 74``` 75