mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2026-02-12 03:04:45 +01:00
#165 new: bio auth
This commit is contained in:
63
lib/core/utils/platform/auth.dart
Normal file
63
lib/core/utils/platform/auth.dart
Normal file
@@ -0,0 +1,63 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:local_auth/local_auth.dart';
|
||||
import 'package:toolbox/core/utils/platform/base.dart';
|
||||
import 'package:local_auth/error_codes.dart' as errs;
|
||||
|
||||
class BioAuth {
|
||||
const BioAuth._();
|
||||
|
||||
static final _auth = LocalAuthentication();
|
||||
|
||||
static bool get isPlatformSupported => isAndroid || isIOS || isWindows;
|
||||
|
||||
static Future<bool> get isAvail async {
|
||||
if (!isPlatformSupported) return false;
|
||||
final canCheckBiometrics = await _auth.canCheckBiometrics;
|
||||
if (!canCheckBiometrics) {
|
||||
return false;
|
||||
}
|
||||
final biometrics = await _auth.getAvailableBiometrics();
|
||||
if (biometrics.isEmpty) return false;
|
||||
return biometrics.contains(BiometricType.fingerprint) ||
|
||||
biometrics.contains(BiometricType.face);
|
||||
}
|
||||
|
||||
static Future<AuthResult> auth([String? msg]) async {
|
||||
if (!await isAvail) return AuthResult.notAvail;
|
||||
try {
|
||||
final reuslt = await _auth.authenticate(
|
||||
localizedReason: msg ?? 'Auth required',
|
||||
options: const AuthenticationOptions(
|
||||
stickyAuth: true,
|
||||
sensitiveTransaction: true,
|
||||
biometricOnly: true,
|
||||
),
|
||||
);
|
||||
if (reuslt) {
|
||||
return AuthResult.success;
|
||||
}
|
||||
return AuthResult.fail;
|
||||
} on PlatformException catch (e) {
|
||||
switch (e.code) {
|
||||
case errs.notEnrolled:
|
||||
return AuthResult.notAvail;
|
||||
case errs.lockedOut:
|
||||
case errs.permanentlyLockedOut:
|
||||
exit(0);
|
||||
}
|
||||
return AuthResult.cancel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum AuthResult {
|
||||
success,
|
||||
// Not match
|
||||
fail,
|
||||
// User cancel
|
||||
cancel,
|
||||
// Device doesn't support biometrics
|
||||
notAvail,
|
||||
}
|
||||
Reference in New Issue
Block a user