mirror of
https://github.com/nisrulz/flutter-examples.git
synced 2025-11-08 20:50:04 +00:00
New Example - Biometrics (#119)
This commit is contained in:
34
biometrics/lib/biometrics_verifier.dart
Normal file
34
biometrics/lib/biometrics_verifier.dart
Normal file
@@ -0,0 +1,34 @@
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:local_auth/local_auth.dart';
|
||||
|
||||
class BiometricsVerifier {
|
||||
late LocalAuthentication auth;
|
||||
|
||||
BiometricsVerifier() {
|
||||
auth = LocalAuthentication();
|
||||
}
|
||||
|
||||
Future<void> _ableToAuthenticate() async {
|
||||
bool supported = await auth.canCheckBiometrics;
|
||||
bool entryExists = await auth.isDeviceSupported();
|
||||
if (!supported) throw "Device doesn't support Biometrics.";
|
||||
if (!entryExists) throw "Biometric entry not found in phone.";
|
||||
}
|
||||
|
||||
Future<void> verifyBiometrics(String? prompt) async {
|
||||
await _ableToAuthenticate();
|
||||
late bool didAuthenticate;
|
||||
try {
|
||||
didAuthenticate = await auth.authenticate(
|
||||
localizedReason: prompt ?? 'Please authenticate with biometrics',
|
||||
options: const AuthenticationOptions(
|
||||
stickyAuth: true,
|
||||
biometricOnly: true,
|
||||
),
|
||||
);
|
||||
} on PlatformException {
|
||||
throw "Platform Exception : Biometrics Failed !";
|
||||
}
|
||||
if (!didAuthenticate) throw "Biometrics Failed";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user