Flutter Biometric Authenticaton
Biometric authentication provides a seamless and secure way to authenticate users using fingerprint or facial recognition. In this blog, we will walk through the process of building a simple biometric authentication app in Flutter that integrates Shared Preferences to store login sessions and enable biometric login only after user authentication.
By the end of this tutorial, you will have a Flutter app where users can:
✅ Register/Login and store their login session in Shared Preferences.
✅ Enable biometric authentication only after logging in manually.
✅ Use fingerprint or face recognition to log in after enabling biometric login.
This blog is divided into two part the first part is logical implementation and second part is Ui Part. First copy paste code from 5️⃣Ui Part and then implement four steps shown in first part
PART I
📂 Project Structure
📂 biometric_auth_app/
│── 📂 android/
│── 📂 ios/
│── 📂 lib/
│ │── 📂 screens/
│ │ │── home_screen.dart
│ │ │── login_screen.dart
│ │── main.dart
│ │── biometric_auth.dart
│── pubspec.yaml
📁 lib/screens/
(Screens Folder)
home_screen.dart
→ Displays a button to enable biometric authentication.login_screen.dart
→ Normal login screen where users enter credentials manually.signup_screen.dart
→ Allows users to sign up.
📁 lib/
(Root Directory)
main.dart
→ Entry point of the Flutter app.biometric_auth.dart
→ Handles biometric authentication logic.
1️⃣Setting Up Dependencies
Run the following command in the terminal:
local_auth
→ Used for fingerprint and face authentication.shared_preferences
→ Used to store the user’s authentication status
2️⃣Configuring Android for Biometric Authentication
Since we are using local_auth
, we need to make some changes in the Android project to support biometric authentication.
a. Modify AndroidManifest.xml
📂 Path: android/app/src/main/AndroidManifest.xml
Open this file and add the following permissions inside the <manifest>
tag:
b.Modify MainActivity.kt
📂 Path:
android/app/src/main/kotlin/com/example/biometric_auth_app/MainActivity.kt
By default, Flutter uses FlutterActivity
, but biometric authentication requires FlutterFragmentActivity
.
Modify MainActivity.kt
to extend FlutterFragmentActivity
Why this change?
FlutterActivity
does not support biometric authentication dialogs properly.FlutterFragmentActivity
allowslocal_auth
to work as expected.
3️⃣Implement biometric authentication
4️⃣Implementing Shared Preferences for Login and Biometric
How It Works?
- When a user logs in, we store a flag (
isLoggedIn = true
) in Shared Preferences. - In the Home Screen, users can enable biometric authentication (
isbiometricenabled
= true
) in Shared Preferences. - If biometric authentication is enabled and user has already logged in earlier, the Login Screen will allow login using a fingerprint.
a. in init state of login screen check shared prefs and decide weather to show biometric or not
b. add biometric authentication optino based on showbiometric bool value
c. set isloggedin value to true on user login
d. update logout button onpressed to clear all stored info in shared pref.
e. In homescreen call checkbiometric func with setup as true