@ledgerhq/react-native-touch-id

React Native Touch ID

React Native Touch ID is a React Native library for authenticating users with biometric authentication methods like Face ID and Touch ID on both iOS and Android (experimental).

⚠️ Note: This library is not currently actively maintained. If you're looking for something more stable that "just works", the awesome folks over at Expo have begun open-sourcing some of their modules for compatability with React Native projects not built with Expo. As such you can attempt to use their equivalent library called LocalAuthentication at expo-local-authentication if you run into any issues here!

Breaking changes

Documentation

Install

npm i --save react-native-touch-id

or

yarn add react-native-touch-id

Support

Due to the rapid changes being made in the React Native ecosystem, we are not officially going to support this module on anything but the latest version of React Native. The current supported version is indicated on the React Native badge at the top of this README. If it's out of date, we encourage you to submit a pull request!

Usage

Linking the Library

In order to use Biometric Authentication, you must first link the library to your project.

Using react-native link

Use the built-in command:

react-native link react-native-touch-id

Using Cocoapods (iOS only)

On iOS you can also link package by updating your podfile

pod 'TouchID',:path=>"#{node_modules_path}/react-native-touch-id"

and then run

pod install

Using native linking

Platform Differences

iOS and Android differ slightly in their TouchID authentication.

On Android you can customize the title and color of the pop-up by passing in the optional config object with a color and title key to the authenticate method. Even if you pass in the config object, iOS does not allow you change the color nor the title of the pop-up. iOS does support passcodeFallback as an option, which when set to true will allow users to use their device pin - useful for people with Face / Touch ID disabled. Passcode fallback only happens if the device does not have touch id or face id enabled.

Error handling is also different between the platforms, with iOS currently providing much more descriptive error codes.

passcodeFallback - iOS - by default set to false. If set to true, will allow use of keypad passcode.

Examples

constoptionalConfigObject={

title:'Authentication Required',// Android

imageColor:'#e00606',// Android

imageErrorColor:'#ff0000',// Android

sensorDescription:'Touch sensor',// Android

sensorErrorDescription:'Failed',// Android

cancelText:'Cancel',// Android

fallbackLabel:'Show Passcode',// iOS (if empty, then label is hidden)

unifiedErrors:false,// use unified error messages (default false)

passcodeFallback:false,// iOS - allows the device to fall back to using the passcode, if faceid/touch is not available. this does not mean that if touchid/faceid fails the first few times it will revert to passcode, rather that if the former are not enrolled, then it will use the passcode.

isSupported()

Returns a Promise that rejects if TouchID is not supported. On iOS resolves with a biometryTypeString of FaceID or TouchID.

Examples

constoptionalConfigObject={

unifiedErrors:false// use unified error messages (default false)

passcodeFallback:false// if true is passed, itwill allow isSupported to return an error if the device is not enrolled in touch id/face id etc. Otherwise, it will just tell you what method is supported, even if the user is not enrolled. (default false)

}

TouchID.isSupported(optionalConfigObject)

.then(biometryType=>{

// Success code

if(biometryType ==='FaceID'){

console.log('FaceID is supported.');

}else{

console.log('TouchID is supported.');

}

})

.catch(error=>{

// Failure code

console.log(error);

});

Errors

There are various reasons why biomentric authentication may not be available or fail. TouchID.isSupported and TouchID.authenticate will return an error representing the reason.

License

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.