How To Implement Face ID and Touch ID in React Native

Learn how to add Face ID and Touch ID to React Native apps using the Local Authentication Framework.

How To Implement  Face ID and Touch ID in React Native

Implementing Face ID and Touch ID in a React Native application involves using specific libraries and APIs that provide the necessary functionality. Here's a general guide to help you get started:

1. Choose the Right Library

  • React Native Biometrics: A popular library for integrating biometric authentication like Face ID and Touch ID.
  • React Native Fingerprint Scanner: Another library that supports both fingerprint and face recognition.

2. Installation

  • Install the chosen library via npm or yarn. For example, for React Native Biometrics:
npm install react-native-biometrics
  • Depending on your React Native version, you might need to link the library manually:
react-native link react-native-biometrics

4. Update Platform-Specific Configurations

  • iOS: Update your Info.plist file to include usage descriptions for Face ID/Touch ID.
<key>NSFaceIDUsageDescription</key>
<string>Why you are using Face ID</string>
  • Android: Update your AndroidManifest.xml to include necessary permissions.

5. Implement Authentication Logic

  • Import the library in your JavaScript file.
  • Use the provided methods to check for biometric capabilities and prompt the user for authentication.
  • Handle success and failure cases appropriately.

6. Testing

  • Test on real devices with Face ID or Touch ID.
  • Ensure you handle cases where the device doesn't support biometrics or the user hasn't set it up.

7. Error Handling

  • Implement robust error handling to manage various scenarios like no biometrics enrolled, user cancellation, etc.

8. Security Considerations

  • Ensure you're using biometrics in a way that complements your app's security.
  • Do not store any biometric data on your server.

9. Update UI/UX

  • Provide clear instructions to the user on how to use biometric authentication.
  • Update the UI dynamically based on the availability of Face ID or Touch ID.

Example Code Snippet

import ReactNativeBiometrics from 'react-native-biometrics'

ReactNativeBiometrics.isSensorAvailable()
  .then((resultObject) => {
    const { available, biometryType } = resultObject

    if (available && biometryType === ReactNativeBiometrics.TouchID) {
      console.log('TouchID is available')
    } else if (available && biometryType === ReactNativeBiometrics.FaceID) {
      console.log('FaceID is available')
    } else if (available) {
      console.log('Biometric sensor is available')
    } else {
      console.log('Biometric sensor is not available')
    }
  })

Documentation and Community Resources

  • Refer to the official documentation of the library you chose for detailed implementation steps.
  • Check out community forums and React Native guides for troubleshooting and advanced usage.

Remember, the specifics can vary based on the library you choose and the versions of React Native and the libraries. Always refer to the most recent documentation for the library you are using.