Skip to main content

Command Palette

Search for a command to run...

Biometric Authentication in Flutter

Published
2 min read
Biometric Authentication in Flutter
E

I’m Emmanuel David Tuksa, a Software Engineer passionate about building impactful digital experiences and solving real-world problems through technology. With a strong foundation in software engineering and a focus on mobile and web solutions, I’m driven to craft user-centric products that scale and delight. I continuously seek opportunities to learn, contribute to open-source, and push the boundaries of what technology can achieve. Whether it’s leading teams, optimizing workflows, or building something from scratch, my goal is simple: create meaningful software that empowers users and teams alike. Let’s build something great together.

Introduction

Authentication simply means verifying beyond a doubt that a person is who they say they are. Biometric authentication performs this verification by checking distinctive biological or behavioral characteristics. While building applications that contain sensitive information and users data, it is important to make it secure and add layers of security, one of such being the biometric authentication. In this article we would go over how to setup biometric authentication in a flutter application.

Create your flutter project

This can be achieved by running:

flutter create project name

Add the loca_auth package

Add local_auth: ^1.1.10 to your pubspec.yaml file. This package is the responsible for the biometric authentication.

After adding the package, run flutter pub get to install the package.

Also change your MainActivity.kt file to a Fragment Activity:

import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant
import android.view.WindowManager.LayoutParams

class MainActivity: FlutterFragmentActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine)
}
}

Next is to add this to AndroidManifest.xml

<uses-permission android:name="android.permission.USE_FINGERPRINT"/

Walk through

The first thing to do is to initialize the LocalAuthentication variable.

final LocalAuthentication localAuthentication = LocalAuthentication();

Next thing to do is to check if the device has biometrics support or not. The localAuthentication variable we declared has a future boolean property canCheckBiometrics that returns true if biometrics is supported and false if not. We can acheive this check with the code below

Future getBiometricSupport() async {
bool hasSupport = false;
try {
hasSupport = await localAuthentication.canCheckBiometrics;
} catch(e) {
print(e);
}
return hasSupport;
}

After checking if the device has biometrics or not, the next thing to do is to authenticate user using biometrics if it is enabled. The code below can be used to achieve this.

Future authenticateUser() async {
bool authenticated = false;
bool biometric = await getBiometricSupport();
setState(() {
hasBiometric = biometric;
});
if (biometric) {
try {
authenticated = await localAuthentication.authenticate(
localizedReason: 'Authenticate User',
useErrorDialogs: true,
stickyAuth: true,
sensitiveTransaction: true,
biometricOnly: true
);
} catch (e) {
print(e);
}
} else {
ScaffoldMessenger.of(context).showSnackBar(errorSnackBar);
setState(() {
authenticated = false;
});
}
return authenticated;
}

This function can be used either on init or on tap of a button depending on the user experience the application desires. With that setup we are all good to go.

Conclusion

This draws us to the end of our short journey on biometric authentication in flutter, I hope it has been really helpful so far. If you have any more questions or want to engage feel free to connect with me on Twitter or LinkedIn. Do appreciate the article with some claps and comments.

If you want the complete code, here is a link to the github repository of the project.

More from this blog

Emmanuel David Tuksa

5 posts

I'm Emmanuel Tuksa, a software engineer sharing lessons from building user-focused apps. I write about what I learn, problems I solve, and tools that help create better products. Tips here. Stay tuned