bio login
This commit is contained in:
1 parent
6acec998bc
commit
0ba83858fe
12 files changed
+522
-80
No files matched your search
@@ -1,26 +1,59 @@
|
||||
class auth_token {
|
||||
// Istanza privata statica
|
||||
static final auth_token _instance = auth_token._internal();
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:local_auth/local_auth.dart';
|
||||
import 'package:device_info_plus/device_info_plus.dart';
|
||||
|
||||
// Variabile per salvare il token
|
||||
class AuthToken {
|
||||
static final AuthToken _instance = AuthToken._internal();
|
||||
static AuthToken get instance => _instance;
|
||||
AuthToken._internal();
|
||||
|
||||
final _storage = const FlutterSecureStorage();
|
||||
String? _token;
|
||||
|
||||
// Costruttore privato
|
||||
auth_token._internal();
|
||||
|
||||
// Factory per ritornare sempre la stessa istanza
|
||||
factory auth_token() {
|
||||
return _instance;
|
||||
}
|
||||
|
||||
// Getter e setter per il token
|
||||
String? get token => _token;
|
||||
set token(String? value) {
|
||||
_token = value;
|
||||
|
||||
Future<bool> isMobilePlatform() async {
|
||||
if (kIsWeb) return false;
|
||||
final deviceInfo = DeviceInfoPlugin();
|
||||
if (defaultTargetPlatform == TargetPlatform.android) return true;
|
||||
if (defaultTargetPlatform == TargetPlatform.iOS) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Metodo per cancellare il token (es. logout)
|
||||
void clear() {
|
||||
Future<void> saveToken(String token) async {
|
||||
_token = token;
|
||||
if (await isMobilePlatform()) {
|
||||
await _storage.write(key: 'AuthToken', value: token);
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> loadTokenWithBiometric() async {
|
||||
if (!await isMobilePlatform()) return false;
|
||||
|
||||
final auth = LocalAuthentication();
|
||||
final canAuth =
|
||||
await auth.canCheckBiometrics || await auth.isDeviceSupported();
|
||||
if (!canAuth) return false;
|
||||
|
||||
final authenticated = await auth.authenticate(
|
||||
localizedReason: 'Autenticati per accedere',
|
||||
options: const AuthenticationOptions(biometricOnly: true),
|
||||
);
|
||||
|
||||
if (authenticated) {
|
||||
final stored = await _storage.read(key: 'AuthToken');
|
||||
_token = stored;
|
||||
return stored != null;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Future<void> clear() async {
|
||||
_token = null;
|
||||
if (await isMobilePlatform()) {
|
||||
await _storage.delete(key: 'AuthToken');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user