浏览代码

create service file for local authentication

ashilkn 2 年之前
父节点
当前提交
1b06395e9c

+ 32 - 0
lib/services/local_authentication_service.dart

@@ -0,0 +1,32 @@
+import 'package:flutter/material.dart';
+import 'package:local_auth/local_auth.dart';
+import 'package:photos/core/configuration.dart';
+import 'package:photos/ui/tools/app_lock.dart';
+import 'package:photos/utils/auth_util.dart';
+import 'package:photos/utils/toast_util.dart';
+
+class LocalAuthenticationService {
+  LocalAuthenticationService._privateConstructor();
+  static final LocalAuthenticationService instance =
+      LocalAuthenticationService._privateConstructor();
+
+  Future<bool> requestLocalAuthentication(
+    BuildContext context,
+    String reason,
+  ) async {
+    if (await LocalAuthentication().isDeviceSupported()) {
+      AppLock.of(context).setEnabled(false);
+      final result = await requestAuthentication(reason);
+      AppLock.of(context).setEnabled(
+        Configuration.instance.shouldShowLockScreen(),
+      );
+      if (!result) {
+        showToast(context, reason);
+        return false;
+      } else {
+        return true;
+      }
+    }
+    return true;
+  }
+}

+ 0 - 23
lib/services/user_service.dart

@@ -3,7 +3,6 @@ import 'dart:typed_data';
 import 'package:dio/dio.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter_sodium/flutter_sodium.dart';
-import 'package:local_auth/local_auth.dart';
 import 'package:logging/logging.dart';
 import 'package:photos/core/configuration.dart';
 import 'package:photos/core/event_bus.dart';
@@ -26,8 +25,6 @@ import 'package:photos/ui/account/password_reentry_page.dart';
 import 'package:photos/ui/account/two_factor_authentication_page.dart';
 import 'package:photos/ui/account/two_factor_recovery_page.dart';
 import 'package:photos/ui/account/two_factor_setup_page.dart';
-import 'package:photos/ui/tools/app_lock.dart';
-import 'package:photos/utils/auth_util.dart';
 import 'package:photos/utils/crypto_util.dart';
 import 'package:photos/utils/dialog_util.dart';
 import 'package:photos/utils/navigation_util.dart';
@@ -849,26 +846,6 @@ class UserService {
     }
   }
 
-  Future<bool> localAuthenticationService(
-    BuildContext context,
-    String reason,
-  ) async {
-    if (await LocalAuthentication().isDeviceSupported()) {
-      AppLock.of(context).setEnabled(false);
-      final result = await requestAuthentication(reason);
-      AppLock.of(context).setEnabled(
-        Configuration.instance.shouldShowLockScreen(),
-      );
-      if (!result) {
-        showToast(context, reason);
-        return false;
-      } else {
-        return true;
-      }
-    }
-    return true;
-  }
-
   Future<void> _saveConfiguration(Response response) async {
     await Configuration.instance.setUserID(response.data["id"]);
     if (response.data["encryptedToken"] != null) {

+ 2 - 1
lib/ui/account/delete_account_page.dart

@@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
 import 'package:flutter_sodium/flutter_sodium.dart';
 import 'package:photos/core/configuration.dart';
 import 'package:photos/models/delete_account.dart';
+import 'package:photos/services/local_authentication_service.dart';
 import 'package:photos/services/user_service.dart';
 import 'package:photos/ui/common/dialogs.dart';
 import 'package:photos/ui/common/gradient_button.dart';
@@ -142,7 +143,7 @@ class DeleteAccountPage extends StatelessWidget {
     DeleteChallengeResponse response,
   ) async {
     final hasAuthenticatedOrNoLocalAuth =
-        await UserService.instance.localAuthenticationService(
+        await LocalAuthenticationService.instance.requestAuthentication(
       context,
       "Please authenticate to initiate account deletion",
     );

+ 4 - 3
lib/ui/settings/account_section_widget.dart

@@ -1,6 +1,7 @@
 import 'package:expandable/expandable.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter_sodium/flutter_sodium.dart';
+import 'package:photos/services/local_authentication_service.dart';
 import 'package:photos/services/user_service.dart';
 import 'package:photos/ui/account/change_email_dialog.dart';
 import 'package:photos/ui/account/password_entry_page.dart';
@@ -36,7 +37,7 @@ class AccountSectionWidgetState extends State<AccountSectionWidget> {
           behavior: HitTestBehavior.translucent,
           onTap: () async {
             final hasAuthenticatedOrNoLocalAuth =
-                await UserService.instance.localAuthenticationService(
+                await LocalAuthenticationService.instance.requestAuthentication(
               context,
               "Please authenticate to view your recovery key",
             );
@@ -69,7 +70,7 @@ class AccountSectionWidgetState extends State<AccountSectionWidget> {
           behavior: HitTestBehavior.translucent,
           onTap: () async {
             final hasAuthenticatedOrNoLocalAuth =
-                await UserService.instance.localAuthenticationService(
+                await LocalAuthenticationService.instance.requestAuthentication(
               context,
               "Please authenticate to change your email",
             );
@@ -94,7 +95,7 @@ class AccountSectionWidgetState extends State<AccountSectionWidget> {
           behavior: HitTestBehavior.translucent,
           onTap: () async {
             final hasAuthenticatedOrNoLocalAuth =
-                await UserService.instance.localAuthenticationService(
+                await LocalAuthenticationService.instance.requestAuthentication(
               context,
               "Please authenticate to change your password",
             );

+ 6 - 4
lib/ui/settings/security_section_widget.dart

@@ -9,6 +9,7 @@ import 'package:photos/core/configuration.dart';
 import 'package:photos/core/event_bus.dart';
 import 'package:photos/ente_theme_data.dart';
 import 'package:photos/events/two_factor_status_change_event.dart';
+import 'package:photos/services/local_authentication_service.dart';
 import 'package:photos/services/user_service.dart';
 import 'package:photos/ui/account/sessions_page.dart';
 import 'package:photos/ui/common/loading_widget.dart';
@@ -81,8 +82,8 @@ class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
                         value: snapshot.data,
                         onChanged: (value) async {
                           final hasAuthenticatedOrNoLocalAuth =
-                              await UserService.instance
-                                  .localAuthenticationService(
+                              await LocalAuthenticationService.instance
+                                  .requestLocalAuthentication(
                             context,
                             "Please authenticate to configure two-factor authentication",
                           );
@@ -253,8 +254,9 @@ class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
       GestureDetector(
         behavior: HitTestBehavior.translucent,
         onTap: () async {
-          final hasAuthenticatedOrNoLocalAuth =
-              await UserService.instance.localAuthenticationService(
+          final hasAuthenticatedOrNoLocalAuth = await LocalAuthenticationService
+              .instance
+              .requestLocalAuthentication(
             context,
             "Please authenticate to view your active sessions",
           );