Local Authentication in Flutter
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
- Local Authentication
- Required Plugin
- Checking for biometric availability
- Checking biometric type
- Authentication using biometric
- 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.
- biometric authentication on iOS
- 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: ^latets version
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
anmolseth06/local_auth
A new Flutter application. This project is a starting point for a Flutter application. A few resources to get you…github.com
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?
- Want to learn advanced Flutter functionality?
- Want to learn job-oriented and high-demand, high-paying flutter iOS and android functionality?
What does this course offer?
- 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.
- 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.
- 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.
- 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.
- 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.
- 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. 🌸