|
1 | 1 | import 'package:flutter/material.dart'; |
2 | 2 | import 'package:flutter_riverpod/flutter_riverpod.dart'; |
3 | | -import 'package:local_auth/local_auth.dart'; |
4 | | -// ignore: depend_on_referenced_packages |
5 | | -import 'package:local_auth_android/local_auth_android.dart'; |
6 | | -// ignore: depend_on_referenced_packages |
7 | | -import 'package:local_auth_darwin/local_auth_darwin.dart'; |
8 | | -// ignore: depend_on_referenced_packages |
9 | | -import 'package:local_auth_windows/local_auth_windows.dart'; |
10 | 3 | import 'package:open_authenticator/i18n/translations.g.dart'; |
11 | 4 | import 'package:open_authenticator/model/crypto.dart'; |
12 | 5 | import 'package:open_authenticator/model/password_verification/methods/method.dart'; |
13 | 6 | import 'package:open_authenticator/model/password_verification/methods/password_signature.dart'; |
14 | 7 | import 'package:open_authenticator/model/password_verification/password_verification.dart'; |
15 | 8 | import 'package:open_authenticator/model/totp/repository.dart'; |
16 | | -import 'package:open_authenticator/utils/platform.dart'; |
| 9 | +import 'package:open_authenticator/utils/local_authentication/local_authentication.dart'; |
17 | 10 | import 'package:open_authenticator/utils/result.dart'; |
18 | 11 | import 'package:open_authenticator/widgets/dialog/text_input_dialog.dart'; |
19 | | -import 'package:window_manager/window_manager.dart'; |
20 | 12 |
|
21 | 13 | /// Allows to unlock the app. |
22 | 14 | sealed class AppUnlockMethod<T> { |
@@ -77,60 +69,17 @@ sealed class CannotUnlockException implements Exception {} |
77 | 69 | class LocalAuthenticationAppUnlockMethod extends AppUnlockMethod { |
78 | 70 | @override |
79 | 71 | Future<Result> _tryUnlock(BuildContext context, Ref ref, UnlockReason reason) async { |
80 | | - LocalAuthentication auth = LocalAuthentication(); |
81 | | - if (currentPlatform.isDesktop) { |
82 | | - await windowManager.ensureInitialized(); |
83 | | - await windowManager.focus(); |
84 | | - await windowManager.setAlwaysOnTop(true); |
85 | | - } |
86 | | - if (!context.mounted) { |
87 | | - return const ResultCancelled(); |
88 | | - } |
89 | | - bool result = await auth.authenticate( |
90 | | - localizedReason: translations.appUnlock.localAuthentication[reason.name] ?? 'Authenticate to access the app.', |
91 | | - authMessages: [ |
92 | | - IOSAuthMessages( |
93 | | - lockOut: translations.localAuth.ios.lockOut, |
94 | | - goToSettingsButton: translations.localAuth.common.goToSettings, |
95 | | - goToSettingsDescription: translations.localAuth.ios.goToSettingsDescription, |
96 | | - cancelButton: MaterialLocalizations.of(context).cancelButtonLabel, |
97 | | - ), |
98 | | - AndroidAuthMessages( |
99 | | - biometricHint: translations.localAuth.android.biometricHint, |
100 | | - biometricNotRecognized: translations.localAuth.android.biometricNotRecognized, |
101 | | - biometricRequiredTitle: translations.localAuth.android.biometricRequiredTitle, |
102 | | - biometricSuccess: translations.error.noError, |
103 | | - cancelButton: MaterialLocalizations.of(context).cancelButtonLabel, |
104 | | - deviceCredentialsRequiredTitle: translations.localAuth.android.deviceCredentialsRequiredTitle, |
105 | | - deviceCredentialsSetupDescription: translations.localAuth.android.deviceCredentialsSetupDescription, |
106 | | - goToSettingsButton: translations.localAuth.common.goToSettings, |
107 | | - goToSettingsDescription: translations.localAuth.android.goToSettingsDescription, |
108 | | - signInTitle: translations.localAuth.android.signInTitle, |
109 | | - ), |
110 | | - const WindowsAuthMessages(), |
111 | | - ], |
112 | | - ); |
113 | | - if (currentPlatform.isDesktop) { |
114 | | - if (currentPlatform == Platform.macOS || currentPlatform == Platform.windows) { |
115 | | - // See https://github.com/flutter/flutter/issues/122322. |
116 | | - await windowManager.blur(); |
117 | | - await windowManager.focus(); |
118 | | - } |
119 | | - await windowManager.setAlwaysOnTop(false); |
120 | | - } |
| 72 | + bool result = await LocalAuthentication.instance.authenticate(context, translations.appUnlock.localAuthentication[reason.name] ?? 'Authenticate to access the app.'); |
121 | 73 | return result ? const ResultSuccess() : const ResultCancelled(); |
122 | 74 | } |
123 | 75 |
|
124 | 76 | @override |
125 | 77 | Future<CannotUnlockException?> canUnlock(Ref ref) async { |
126 | | - if (!(await isSupported())) { |
| 78 | + if (!(await LocalAuthentication.instance.isSupported())) { |
127 | 79 | return LocalAuthenticationDeviceNotSupported(); |
128 | 80 | } |
129 | 81 | return null; |
130 | 82 | } |
131 | | - |
132 | | - /// Returns whether this unlock method is supported; |
133 | | - static Future<bool> isSupported() => LocalAuthentication().isDeviceSupported(); |
134 | 83 | } |
135 | 84 |
|
136 | 85 | /// Indicates that local authentication is not supported by the device. |
|
0 commit comments