Biometrics Authentication with Face ID and Fingerprint in React Native

Biometrics authentication has become a ubiquitous security measure for mobile applications. It allows users to quickly and securely authenticate their identity without the need for passwords or other credentials. React Native provides support for both Face ID and Fingerprint authentication on iOS and Android devices. In this tutorial, we will discuss how to use biometrics authentication with Face ID and Fingerprint in React Native.

react native biometrics face id

Prerequisites

Before getting started with this tutorial, you should have a basic understanding of React Native and the concepts of biometrics authentication. Additionally, you should have the following installed on your system:

  • Node.js
  • Xcode or Android Studio
  • React Native CLI

Creating a React Native Project

To create a React Native project, open up a terminal window and type the following command:

react-native init MyBiometricsProject

This will create a new React Native project called MyBiometricsProject.

Installing The Dependencies

Now that we have our project set up, we need to install the dependencies that will allow us to use biometrics authentication. To do this, open up a terminal window and navigate to the project directory. Then, type the following command:

npm install react-native-local-authentication

This will install the react-native-local-authentication package, which provides support for biometrics authentication.

Getting Face ID and Fingerprint Permissions in React Native

In order to use biometrics authentication, we need to request permission from the user. To do this, we can use the LocalAuthentication API from the react-native-local-authentication package. First, we need to import the API into our React Native project:

import LocalAuthentication from 'react-native-local-authentication';

Then, we can use the authenticate method to request permission from the user:

LocalAuthentication.authenticate({
  promptMessage: 'Please authenticate your identity',
  fallbackLabel: 'Use Passcode',
  disableDeviceFallback: true,
});

The authenticate method takes an object as an argument, which can be used to customize the authentication prompt. For example, we can specify a prompt message and a fallback label. The disableDeviceFallback option can be used to prevent the user from using a passcode instead of biometrics authentication.

How to Use Biometrics in React Native

Once we have requested permission from the user, we can use the authenticate method to authenticate the user’s identity. The authenticate method takes a callback function as an argument, which will be called when the authentication process is completed. The callback function takes an object as an argument, which contains information about the authentication process. We can use this object to determine whether or not the authentication process was successful:

LocalAuthentication.authenticate({
  promptMessage: 'Please authenticate your identity',
  fallbackLabel: 'Use Passcode',
  disableDeviceFallback: true,
}, (error, success) => {
  if (error) {
    // Authentication failed
  } else {
    // Authentication successful
  }
});

If the authentication process was successful, we can proceed with the application logic. Otherwise, we can prompt the user to try again or use a different authentication method.

Conclusion

In this tutorial, we discussed how to use biometrics authentication with Face ID and Fingerprint in React Native. We discussed how to request permission from the user and how to authenticate the user’s identity. We also discussed how to handle errors and how to proceed with the application logic once the authentication process is successful. With the help of this tutorial, you should now be able to implement biometrics authentication in your React Native applications.