mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 07:14:28 +01:00
try: fix repeated auth (#294)
This commit is contained in:
@@ -2,14 +2,22 @@ import 'dart:io';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:local_auth/local_auth.dart';
|
||||
// ignore: depend_on_referenced_packages
|
||||
import 'package:local_auth_android/local_auth_android.dart';
|
||||
// ignore: depend_on_referenced_packages
|
||||
import 'package:local_auth_ios/types/auth_messages_ios.dart';
|
||||
import 'package:toolbox/core/extension/context/locale.dart';
|
||||
import 'package:toolbox/core/utils/platform/base.dart';
|
||||
import 'package:local_auth/error_codes.dart' as errs;
|
||||
import 'package:toolbox/data/res/store.dart';
|
||||
|
||||
abstract final class BioAuth {
|
||||
static final _auth = LocalAuthentication();
|
||||
|
||||
static bool get isPlatformSupported => isAndroid || isIOS || isWindows;
|
||||
|
||||
static bool _isAuthing = false;
|
||||
|
||||
static Future<bool> get isAvail async {
|
||||
if (!isPlatformSupported) return false;
|
||||
if (!await _auth.canCheckBiometrics) {
|
||||
@@ -24,16 +32,55 @@ abstract final class BioAuth {
|
||||
biometrics.contains(BiometricType.fingerprint);
|
||||
}
|
||||
|
||||
static Future<AuthResult> auth([String? msg]) async {
|
||||
static void auth([int count = 0]) async {
|
||||
if (Stores.setting.useBioAuth.fetch()) {
|
||||
if (!_isAuthing) {
|
||||
_isAuthing = true;
|
||||
final val = await authWithResult();
|
||||
switch (val) {
|
||||
case AuthResult.success:
|
||||
// wait for animation
|
||||
Future.delayed(
|
||||
count >= 3 ? const Duration(milliseconds: 500) : const Duration(seconds: 1),
|
||||
() => _isAuthing = false,
|
||||
);
|
||||
break;
|
||||
case AuthResult.fail:
|
||||
case AuthResult.cancel:
|
||||
_isAuthing = false;
|
||||
auth(count + 1);
|
||||
break;
|
||||
case AuthResult.notAvail:
|
||||
_isAuthing = false;
|
||||
Stores.setting.useBioAuth.put(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static Future<AuthResult> authWithResult() async {
|
||||
if (!await isAvail) return AuthResult.notAvail;
|
||||
try {
|
||||
await _auth.stopAuthentication();
|
||||
final reuslt = await _auth.authenticate(
|
||||
localizedReason: msg ?? 'Auth required',
|
||||
options: const AuthenticationOptions(
|
||||
stickyAuth: true,
|
||||
biometricOnly: true,
|
||||
),
|
||||
);
|
||||
localizedReason: l10n.authRequired,
|
||||
options: const AuthenticationOptions(
|
||||
biometricOnly: true,
|
||||
),
|
||||
authMessages: [
|
||||
AndroidAuthMessages(
|
||||
biometricHint: l10n.bioAuth,
|
||||
biometricNotRecognized: l10n.failed,
|
||||
biometricRequiredTitle: l10n.authRequired,
|
||||
biometricSuccess: l10n.success,
|
||||
cancelButton: l10n.cancel,
|
||||
),
|
||||
IOSAuthMessages(
|
||||
lockOut: l10n.authRequired,
|
||||
cancelButton: l10n.ok,
|
||||
),
|
||||
]);
|
||||
if (reuslt) {
|
||||
return AuthResult.success;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user