Local Authentication with Flutter

In this post I am going to implement this feature using flutter and dart.




First of all download and install flutter SDK from

https://flutter.dev/docs/get-started/install/windows


Create a project:
Enter this command in command prompt  "flutter create signin_flutter"


Importing packages:
Now import the plug-in local_auth by adding the following line to your project's pubspec.yaml file.

"loacl_auth: ^0.4.0+1."


you can also find latest version on https://pub.dev/packages/local_auth

Adding Permissions:

In order to have authentication working correctly on android we have to update AndriodManifesto.xml file to include the USE_FINGERPRINT permission.

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

we can find the file here
Your project directory...\signin_flutter\android\app\src\main\AndroidManifest.xml


Adding local Authentication:

import local_auth package in main.dart file
import 'package:local_auth/local_auth.dart';

and create an instance
final LocalAuthentication _localAuthentication = LocalAuthentication();


Now we will be implementing 3 methods:

1. _checkBiometric()  for checking bio metric authentication hardware is available in the device.
2. _getListOfBiometricTypes()  for getting the list of available bio metric types.
3._authorizeNow()  for authenticating the user.

  bool _canCheckBiometric = false;  
  String _authorizedOrNot = "Not Authorized";  
  List<BiometricType> _availableBiometricTypes = List<BiometricType>();  
  Future<void> _checkBiometric() async {  
   bool canCheckBiometric = false;  
   try {  
    canCheckBiometric = await _localAuthentication.canCheckBiometrics;  
   } on PlatformException catch (e) {  
    print(e);  
   }  
   if (!mounted) return;  
   setState(() {  
    _canCheckBiometric = canCheckBiometric;  
   });  
  }  
  Future<void> _getListOfBiometricTypes() async {  
   List<BiometricType> listofBiometrics;  
   try {  
    listofBiometrics = await _localAuthentication.getAvailableBiometrics();  
   } on PlatformException catch (e) {  
    print(e);  
   }  
   if (!mounted) return;  
   setState(() {  
    _availableBiometricTypes = listofBiometrics;  
   });  
  }  
  Future<void> _authorizeNow() async {  
   bool isAuthorized = false;  
   try {  
    isAuthorized = await _localAuthentication.authenticateWithBiometrics(  
     localizedReason: "Please authenticate proceed",  
     useErrorDialogs: true,  
     stickyAuth: true,  
    );  
   } on PlatformException catch (e) {  
    print(e.message);  
   }  
   if (!mounted) return;  
   setState(() {  
    if (isAuthorized) {  
     _authorizedOrNot = "Authorized";  
    } else {  
     _authorizedOrNot = "Not Authorized";  
    }  
   });  
  }  

we need to import
import 'package:flutter/services.dart';
for getting PlatformExceptions.


Building a basic UI:

we are adding sample
3 text widgets and respective buttons.

 Scaffold(  
    appBar: AppBar(  
     title: Text(widget.title),  
    ),  
    body: Center(  
     child: Column(  
      mainAxisAlignment: MainAxisAlignment.center,  
      children: <Widget>[  
       Text("Can we check Biometric : $_canCheckBiometric"),  
       RaisedButton(  
        onPressed: _checkBiometric,  
        child: Text("Check Biometric"),  
        color: Colors.blue,  
        colorBrightness: Brightness.light,  
       ),  
       Text("List Of Biometric : ${_availableBiometricTypes.toString()}"),  
       RaisedButton(  
        onPressed: _getListOfBiometricTypes,  
        child: Text("List of Biometric Types"),  
        color: Colors.blue,  
        colorBrightness: Brightness.light,  
       ),  
       Text("Authorized : $_authorizedOrNot"),  
       RaisedButton(  
        onPressed: _authorizeNow,  
        child: Text("Authorize now"),  
        color: Colors.blue,  
        colorBrightness: Brightness.light,  
       ),  
      ],  
     ),  
    ),  
   );  

UI Screens:




Conclusion:

I hope this article will saves your time to integrate bio metric authentication with flutter.

you can find the source code from my GitHub repository here



Comments

Popular posts from this blog

Offline Nuget packages

Internationalizing Flutter apps