# react-native-local-authentication

Local authentication for react native, supports iOS only. See [iOS Local Authentication Document](https://developer.apple.com/documentation/localauthentication)


<!-- @import "[TOC]" {cmd="toc" depthFrom=1 depthTo=6 orderedList=false} -->
<!-- code_chunk_output -->

* [react-native-local-authentication](#react-native-local-authentication)
	* [Install](#install)
	* [Usage](#usage)
		* [`setLocalizedFallbackTitle(title)`](#setlocalizedfallbacktitletitle)
		* [`setLocalizedCancelTitle(title)`](#setlocalizedcanceltitletitle)
		* [`canEvaluate(policy)`](#canevaluatepolicy)
			* [Parameters](#parameters)
			* [Response](#response)
		* [`evaluate(policy, localizedReason)`](#evaluatepolicy-localizedreason)
			* [parameters](#parameters-1)
			* [Response](#response-1)
		* [`invalidate()`](#invalidate)
		* [LAError](#laerror)
	* [Example](#example)

<!-- /code_chunk_output -->


## Install

```sh
npm i --save react-native-local-authentication
react-native link react-native-local-authentication
```

## Usage

### `setLocalizedFallbackTitle(title)`

Set fallback button title.

### `setLocalizedCancelTitle(title)`

Set cancel button title.

### `canEvaluate(policy)`

Determines if a particular policy can be evaluated.

#### Parameters

* policy

The policy to evaluate.

see [LAPolicy](https://developer.apple.com/documentation/localauthentication/lapolicy)

#### Response

Successful

see [LABiometryType](https://developer.apple.com/documentation/localauthentication/labiometrytype)

* none: No biometry type is supported.
* touchID: The device supports Touch ID.
* faceID: The device supports Face ID.

Failed

* code: see [LAError](#laerror)
* message: error message

### `evaluate(policy, localizedReason)`

#### parameters

* policy

Policy for which the preflight check should be run.

* localizedReason

Policy for which the preflight check should be run.

#### Response

Successful

return true

Failed

* code: see [LAError](#laerror)
* message: error message

### `invalidate()`

Invalidates the context.

### LAError

error code

* [LAErrorAuthenticationFailed](https://developer.apple.com/documentation/localauthentication/laerror/LAErrorAuthenticationFailed)
* [LAErrorUserCancel](https://developer.apple.com/documentation/localauthentication/laerror/LAErrorUserCancel)
* [LAErrorUserFallback](https://developer.apple.com/documentation/localauthentication/laerror/LAErrorUserFallback)
* [LAErrorSystemCancel](https://developer.apple.com/documentation/localauthentication/laerror/LAErrorSystemCancel)
* [LAErrorPasscodeNotSet](https://developer.apple.com/documentation/localauthentication/laerror/LAErrorPasscodeNotSet)
* [LAErrorTouchIDNotAvailable](https://developer.apple.com/documentation/localauthentication/laerror/LAErrorTouchIDNotAvailable) `DEPRECATED`
* [LAErrorTouchIDNotEnrolled](https://developer.apple.com/documentation/localauthentication/laerror/LAErrorTouchIDNotEnrolled) `DEPRECATED`
* [LAErrorTouchIDLockout](https://developer.apple.com/documentation/localauthentication/laerror/LAErrorTouchIDLockout) `DEPRECATED`
* [LAErrorAppCancel](https://developer.apple.com/documentation/localauthentication/laerror/LAErrorAppCancel)
* [LAErrorInvalidContext](https://developer.apple.com/documentation/localauthentication/laerror/LAErrorInvalidContext)
* [LAErrorNotInteractive](https://developer.apple.com/documentation/localauthentication/laerror/LAErrorNotInteractive)
* [LAErrorBiometryNotAvailable](https://developer.apple.com/documentation/localauthentication/laerror/LAErrorBiometryNotAvailable)
* [LAErrorBiometryNotEnrolled](https://developer.apple.com/documentation/localauthentication/laerror/LAErrorBiometryNotEnrolled)
* [LAErrorBiometryLockout](https://developer.apple.com/documentation/localauthentication/laerror/LAErrorBiometryLockout)


## Example

```js
import RNLocalAuthentication, { LAError, LABiometryType } from 'react-native-local-authentication';

async _evaluate() {
  try {
    const biometryType = await RNLocalAuthentication.canEvaluate();
    if (biometryType && (biometryType === LABiometryType.touchID || LABiometryType.faceID)) {
      const result = await RNLocalAuthentication.evaluate(1, biometryType === LABiometryType.touchID ? 'Evaluate Touch ID' : 'Evaluate Face ID');
      console.log('evaluate', result ? 'evaluate successful' : 'evaluate failed');
      Alert.alert('evaluate', result ? 'successful' : 'failed');
    } else {
      Alert.alert('Can not evaluate', biometryType);
    }
  } catch (e) {
    console.log('evaluate', e.code, e.message, e);
    if (parseInt(e.code) === LAError.LAErrorTouchIDNotAvailable) {
      Alert.alert('Error', 'device does not support touch ID or face ID');
    } else {
      Alert.alert('Error', e.message);
    }
  }
};
```
