Local Authentication in Flutter

Anmol Gupta
FlutterDevs
Published in
5 min readMay 13, 2020

--

Introduction

Hello, I welcome you all to my new post on Local Authentication in Flutter. In this post, we shall discuss how to implement the FingerPrint in your flutter app for local authentication. So let’s start with the context.

Table of content

  1. Local Authentication
  2. Required Plugin
  3. Checking for biometric availability
  4. Checking biometric type
  5. Authentication using biometric
  6. Designing a simple App

Local Authentication

Verifying the user locally i.e. without the use of a web server, using facelock or fingerprint can be termed as Local Authentication.

  1. biometric authentication on iOS
  2. fingerprint APIs on Android

Required Plugin

Fortunately, we have a plugin managed by the flutter team.

Just go to pub.dev and search for local_auth package.

Install the dependencies in your pubspec.yaml file.

dependencies:
local_auth: ^0.4.0+1

install the dependence

Useflutter_pub_get command to install the dependence.

import the package

import 'package:local_auth/local_auth.dart';

Checking for biometric availability

final LocalAuthentication auth = LocalAuthentication();
bool canCheckBiometrics = false;
try {
canCheckBiometrics = await auth.canCheckBiometrics;
} catch (e) {
print("error biome trics $e");
}

print("biometric is available: $canCheckBiometrics");

We are using the try and catch method for checking biometric and catching errors.

initializing LocalAuthentication

final LocalAuthentication auth = LocalAuthentication();

canCheckBiometrics

bool canCheckBiometrics = false;

We have made a bool variable canCheckBiometrics which is initially set to false.

We are using it to set/update the availability of biometric.

auth.canCheckBiometrics

Here canCheckBiometrics is a method of LocalAuthentication() object which returns a boolean value.

Checking biometric type

List<BiometricType> availableBiometrics;
try {
availableBiometrics = await auth.getAvailableBiometrics();
} catch (e) {
print("error enumerate biometrics $e");
}

print("following biometrics are available");
if (availableBiometrics.isNotEmpty) {
availableBiometrics.forEach((ab) {
print("Avalible Biomatrics: $ab");
});
} else {
print("no biometrics are available");
}

getAvailableBiometrics() method returns the list of available Biometrics.

Authentication using biometric

bool authenticated = false;
try {
authenticated = await auth.authenticateWithBiometrics(
localizedReason: 'Touch your finger on the sensor to login',
useErrorDialogs: true,
stickyAuth: false,
androidAuthStrings:
AndroidAuthMessages(signInTitle: "Login to HomePage"));
} catch (e) {
print("error using biometric auth: $e");
}
print("authenticated: $authenticated");

authenticateWithBiometrics() is a method that returns a dialog box to provide a message to the user to press the fingerprint sensor for authentication.

To read more about each method you can press Ctrl+B in that particular method.

Full method

void _checkBiometric() async {
final LocalAuthentication auth = LocalAuthentication();
bool canCheckBiometrics = false;
try {
canCheckBiometrics = await auth.canCheckBiometrics;
} catch (e) {
print("error biome trics $e");
}

print("biometric is available: $canCheckBiometrics");

List<BiometricType> availableBiometrics;
try {
availableBiometrics = await auth.getAvailableBiometrics();
} catch (e) {
print("error enumerate biometrics $e");
}

print("following biometrics are available");
if (availableBiometrics.isNotEmpty) {
availableBiometrics.forEach((ab) {
print("\ttech: $ab");
});
} else {
print("no biometrics are available");
}

bool authenticated = false;
try {
authenticated = await auth.authenticateWithBiometrics(
localizedReason: 'Touch your finger on the sensor to login',
useErrorDialogs: true,
stickyAuth: false,
androidAuthStrings:
AndroidAuthMessages(signInTitle: "Login to HomePage"));
} catch (e) {
print("error using biometric auth: $e");
}
setState(() {
isAuth = authenticated ? true : false;
});

print("authenticated: $authenticated");
}

Designing a simple App

Github link

Hi, I am Anmol Gupta, a Flutter Developer, content writer, and now udemy instructor. I am a passionate application developer and curious learner and I love sharing my knowledge with others that’s why I decided to write on medium, and now I am expanding my capabilities by creating a demanding udemy course that will help Flutter Developer to learn advanced functionalities that will directly help you in landing high paying job or getting high paying clients internationally.

Who is this course for?

Want to build Flutter apps with native functionalities?

  1. Want to learn advanced Flutter functionality?
  2. Want to learn job-oriented and high-demand, high-paying flutter iOS and android functionality?

What does this course offer?

  1. Flutter Razorpay payment gateway integration: In this module, we shall learn about Razorpay payment gateway integration which will help us to process the payment of the purchases made by the user.
  2. Flutter Stripe payment gateway integration: In this module, we shall learn about Stripe payment gateway integration which will help us to process the payment of the purchases made by the user.
  3. FLUTTER SCAN AND GENERATE QR CODE: In this module, we shall learn how we can scan a QR code and generate the QR code in Flutter.
  4. FLUTTER FIREBASE EMAIL PASSWORD AUTHENTICATION: In this module, we will learn how we can perform authentication using an email password provider, we will also learn how we can verify the user’s email and reset the password.
  5. FLUTTER FIREBASE PHONE AUTHENTICATION: In this module, we will learn how we can perform authentication using a phone authentication provider, and we will see how we can send the OTP to the provided phone number and sign in using the phone credentials.
  6. FLUTTER FIREBASE GOOGLE AUTHENTICATION: In this module, we will learn how to perform authentication using the google authentication provider using firebase in flutter.

Thanks for reading this article ❤

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.

If we got something wrong? Let me know in the comments. we would love to improve.

Feel free to connect with us:
And read more articles from FlutterDevs.com

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on Facebook, GitHub, Twitter and linkedIn for any flutter related queries.

We welcome feedback, and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences!.

Thank you for reading. 🌸

--

--