Files
flutter-examples/biometrics/lib/biometrics_verifier.dart
2022-10-22 20:33:44 +02:00

35 lines
1.0 KiB
Dart

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";
}
}