Biometrics Authentication with Face ID and Fingerprint in Flutter

Biometrics authentication is a convenient way to validate user identity and provide secure access to applications. It is becoming increasingly popular in mobile apps due to the convenience and security it provides. In this article, we will look at how to implement biometrics authentication with Face ID and Fingerprint in Flutter.

flutter biometrics face id

Prerequisites

Before getting started, you should have a basic understanding of Flutter and the concepts of authentication. Additionally, you will need to have the latest version of the Flutter SDK installed.

Creating a Flutter Project

Let's start by creating a new Flutter project. Open the terminal, navigate to a suitable directory, and enter the following command:

flutter create biometrics_auth

This will create a new Flutter project with the name biometrics_auth.

Installing The Dependencies

Next, we need to install the local_auth package, which will enable us to use biometrics authentication in our Flutter app. To install it, open the pubspec.yaml file and add the following line:

dependencies:
  local_auth: ^0.6.2

Then, run the following command in the terminal to install the package:

flutter pub get

Getting Face ID and Fingerprint Permissions in Flutter

We need to request permission from the user to use Face ID and Fingerprint authentication. To do this, we need to use the LocalAuthentication class, which is part of the local_auth package.

First, we need to create an instance of the LocalAuthentication class. We can do this by calling the LocalAuthentication.instance method:

final localAuth = LocalAuthentication.instance;

Next, we need to call the authenticateWithBiometrics method, which will prompt the user to provide permission to use Face ID or Fingerprint authentication. This method takes a BiometricAuthenticationRequest object as an argument, which we can create using the BiometricAuthenticationRequest.withProtectionLevel constructor:

BiometricAuthenticationRequest request = 
  BiometricAuthenticationRequest.withProtectionLevel(
    BiometricProtectionLevel.encryption,
);

The BiometricProtectionLevel.encryption argument specifies that the user data should be encrypted when stored on the device.

Finally, we can call the authenticateWithBiometrics method, passing in the request object:

bool authenticated = 
  await localAuth.authenticateWithBiometrics(request);

This method will return a boolean value indicating whether the user has granted permission to use Face ID or Fingerprint authentication.

How to Use Biometrics in Flutter

Once we have permission to use biometrics authentication, we can use the authenticateWithBiometrics method to authenticate the user. This method takes a BiometricAuthenticationRequest object as an argument, which we can create using the BiometricAuthenticationRequest.withProtectionLevel constructor:

BiometricAuthenticationRequest request = 
  BiometricAuthenticationRequest.withProtectionLevel(
    BiometricProtectionLevel.encryption,
);

The BiometricProtectionLevel.encryption argument specifies that the user data should be encrypted when stored on the device.

Next, we can call the authenticateWithBiometrics method, passing in the request object:

bool authenticated = 
  await localAuth.authenticateWithBiometrics(request);

This method will return a boolean value indicating whether the user has been successfully authenticated.

Conclusion

In this article, we looked at how to implement biometrics authentication with Face ID and Fingerprint in Flutter. We discussed the prerequisites and how to get permission from the user to use biometrics authentication. We also looked at how to authenticate the user with biometrics.