diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index d2fa5c6d3..e67912b1a 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -856,11 +856,13 @@ "@scanBiometricHint": {}, "scanLibrary": "Scan library", "@scanLibrary": {}, - "scanYourFingerprintToAuthenticate": "Scan your fingerprint to authenticate {user}", - "@scanYourFingerprintToAuthenticate": { + "authenticateWithBiometrics": "Use biometrics to authenticate {user}", + "@authenticateWithBiometrics": { + "description": "Prompt shown to the user when biometric authentication is requested", "placeholders": { "user": { - "type": "String" + "type": "String", + "example": "Jane" } } }, diff --git a/lib/screens/shared/authenticate_button_options.dart b/lib/screens/shared/authenticate_button_options.dart index 5fc86ce15..f75c487d5 100644 --- a/lib/screens/shared/authenticate_button_options.dart +++ b/lib/screens/shared/authenticate_button_options.dart @@ -34,7 +34,11 @@ Future showAuthOptionsDialogue( setMethod.call(currentUser.copyWith(authMethod: method)); break; case Authentication.biometrics: - final authenticated = await AuthService.authenticateUser(context, currentUser); + final authenticated = await AuthService.authenticateUser( + context, + currentUser, + sensitiveTransaction: true, + ); if (authenticated) { setMethod.call(currentUser.copyWith(authMethod: method)); } else if (context.mounted) { diff --git a/lib/util/auth_service.dart b/lib/util/auth_service.dart index ab50159bb..54c7569dc 100644 --- a/lib/util/auth_service.dart +++ b/lib/util/auth_service.dart @@ -1,5 +1,7 @@ // ignore_for_file: depend_on_referenced_packages +import 'dart:developer'; + import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -11,15 +13,21 @@ import 'package:fladder/models/account_model.dart'; import 'package:fladder/util/localization_helper.dart'; class AuthService { - static Future authenticateUser(BuildContext context, AccountModel user) async { + static Future authenticateUser( + BuildContext context, + AccountModel user, { + bool stickyAuth = true, + bool sensitiveTransaction = false, + }) async { final LocalAuthentication localAuthentication = LocalAuthentication(); bool isAuthenticated = false; bool isBiometricSupported = await localAuthentication.isDeviceSupported(); + if (isBiometricSupported) { try { isAuthenticated = await localAuthentication.authenticate( localizedReason: - context.localized.scanYourFingerprintToAuthenticate("(${user.name} - ${user.credentials.serverName})"), + context.localized.authenticateWithBiometrics("(${user.name} - ${user.credentials.serverName})"), authMessages: [ AndroidAuthMessages( signInTitle: 'Fladder', @@ -29,8 +37,17 @@ class AuthService { cancelButton: context.localized.cancel, ) ], + options: AuthenticationOptions( + stickyAuth: stickyAuth, + sensitiveTransaction: sensitiveTransaction, + ), ); - } on PlatformException catch (_) {} + } on PlatformException catch (e) { + debugPrint('Error during authentication: $e'); + } + } else { + log('Biometric authentication is not supported on this device.'); + return false; } return isAuthenticated; }