skip to Main Content

Is local_auth package, biometric faceId is working on both Android and IOS ?, i tried many times, but in android only working fingerprint only, faceID is active on my device but still coming fingerprint option anyone know this reason

I tried to work on faceID In android, i gotting mixed review on this topic

2

Answers


  1. Here is my code for both working (Android and iOS)

    local_auth: ^2.1.6
    
      ValueListenableBuilder(
              valueListenable: _authList,
              builder: (context, valueData, _) {
                return ((Platform.isIOS &&
                            _authList.value.contains(BiometricType.face)) ||
                        (Platform.isAndroid &&
                            _authList.value.contains(BiometricType.weak)))
                    ? ValueListenableBuilder<bool>(
                        valueListenable: isLoginWithFaceId,
                        builder: (context, valueData, _) {
                          return InkWell(
                            onTap: () async {
                              if (LoginUtils.login
                                      .getLoginEmail()
                                      ?.isNotEmpty ??
                                  false) {
                                final result = await 
                            _localAuth.authenticate(
                                  localizedReason:
                                      'Please authenticate to login',
                                  options: const AuthenticationOptions(
                                      stickyAuth: true),
                                );
                                if (result) {
                                  isLoginWithFaceId.value = true;
                                  isLoginWithFaceId.notifyListeners();
    
                                  removeKeyPad();
                                  _loginApiCall();
                                }
                              } else {
                                showCustomDialog(
                                    context,
                                    'You need to login once to enable FaceID. So first time login with credentials.',
                                    StringHelper.ok);
                              }
                            },
                            child:
                                Image.asset(ImageHelper.faceLock, height: 
                                 32.0),
                          );
                        })
                    : SizedBox.shrink();
              }),
    
    
     final LocalAuthentication _localAuth = LocalAuthentication();
     final ValueNotifier<List<BiometricType>> _authList = ValueNotifier([]);
    
    Login or Signup to reply.
  2. The local_auth package in Flutter primarily supports biometric authentication, including fingerprint and facial recognition. However, the support for face recognition on Android devices can be inconsistent due to hardware and software variations across different Android manufacturers.

    Here’s a comprehensive guide to using the local_auth package for biometric authentication in Flutter and understanding its limitations:

    Step-by-Step Guide to Implement Biometric Authentication

    Step 1: Add Dependencies

    Add the local_auth package to your pubspec.yaml file:

    dependencies:
      flutter:
        sdk: flutter
      local_auth: ^2.1.3
    

    Step 2: Update Platform-Specific Configurations

    Android Configuration:

    1. Add Permissions in AndroidManifest.xml:
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.your_app">
    
        <uses-permission android:name="android.permission.USE_BIOMETRIC"/>
        <uses-permission android:name="android.permission.USE_FINGERPRINT"/>
        <uses-permission android:name="android.permission.INTERNET"/>
        
        <application
            ...
            android:usesCleartextTraffic="true">
            ...
        </application>
    </manifest>
    
    1. Add Biometric Authentication Configuration in android/app/src/main/res/xml/authenticator.xml:

    Create a file named authenticator.xml in the res/xml directory and add the following:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.your_app">
    
        <uses-permission android:name="android.permission.USE_BIOMETRIC"/>
        <uses-permission android:name="android.permission.USE_FINGERPRINT"/>
        <uses-permission android:name="android.permission.INTERNET"/>
        
        <application
            ...
            android:usesCleartextTraffic="true">
            ...
        </application>
    </manifest>
    

    iOS Configuration:

    1. Update Info.plist:
    <key>NSFaceIDUsageDescription</key>
    <string>We use Face ID to secure your data</string>
    

    Step 3: Implement Biometric Authentication

    Create a Dart file to handle biometric authentication:

    import 'package:flutter/material.dart';
    import 'package:local_auth/local_auth.dart';
    
    class BiometricAuthentication extends StatefulWidget {
      @override
      _BiometricAuthenticationState createState() => _BiometricAuthenticationState();
    }
    
    class _BiometricAuthenticationState extends State<BiometricAuthentication> {
      final LocalAuthentication auth = LocalAuthentication();
    
      @override
      void initState() {
        super.initState();
        _checkBiometrics();
      }
    
      Future<void> _checkBiometrics() async {
        bool canCheckBiometrics = await auth.canCheckBiometrics;
        List<BiometricType> availableBiometrics = await auth.getAvailableBiometrics();
    
        if (canCheckBiometrics) {
          print("Biometrics are available.");
          print("Available biometrics: $availableBiometrics");
    
          // Check for specific biometric types
          if (availableBiometrics.contains(BiometricType.face)) {
            print("Face ID is available");
            _authenticateWithBiometrics();
          } else if (availableBiometrics.contains(BiometricType.fingerprint)) {
            print("Fingerprint is available");
            _authenticateWithBiometrics();
          } else {
            print("No supported biometrics available");
          }
        } else {
          print("Biometrics are not available.");
        }
      }
    
      Future<void> _authenticateWithBiometrics() async {
        bool authenticated = false;
        try {
          authenticated = await auth.authenticate(
            localizedReason: 'Scan your biometric to authenticate',
            options: AuthenticationOptions(
              biometricOnly: true,
            ),
          );
        } catch (e) {
          print(e);
        }
    
        if (!mounted) return;
    
        setState(() {
          if (authenticated) {
            print("Authentication successful");
          } else {
            print("Authentication failed");
          }
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Biometric Authentication'),
          ),
          body: Center(
            child: ElevatedButton(
              onPressed: _checkBiometrics,
              child: Text('Authenticate'),
            ),
          ),
        );
      }
    }
    

    Explanation and Common Issues

    1. Checking Biometrics:

      • The canCheckBiometrics method checks if any biometric hardware is available on the device.
      • The getAvailableBiometrics method returns a list of available biometric types (face, fingerprint).
    2. Authenticating:

      • The authenticate method triggers the biometric authentication prompt.
    3. Common Issues on Android:

      • Hardware and OS Limitations: Not all Android devices support face recognition through the local_auth package. This feature is more consistent on iOS.
      • Security Policies: Some Android devices may prioritize fingerprint authentication over face recognition due to higher security policies.

    Testing

    • Android:
      • Ensure your device supports face recognition and it is properly set up.
      • Test on multiple devices to account for hardware variations.
    • iOS:
      • Face ID should work if the device supports it (e.g., iPhone X and later).

    By following this setup, you should be able to implement biometric authentication in your Flutter app. However, do keep in mind the limitations and differences in biometric support across different devices and platforms.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search