mirror of
https://github.com/nisrulz/flutter-examples.git
synced 2025-07-04 17:50:04 +08:00
35 lines
1.0 KiB
Dart
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";
|
|
}
|
|
}
|