diff --git a/lib/main.dart b/lib/main.dart index 80d8c50..12634d8 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -7,6 +7,7 @@ import 'package:flutter/material.dart'; import 'package:app_links/app_links.dart'; import 'package:rate_my_app/rate_my_app.dart'; import 'package:traccar_client/geolocation_service.dart'; +import 'package:traccar_client/password_service.dart'; import 'package:traccar_client/push_service.dart'; import 'package:traccar_client/quick_actions.dart'; @@ -23,6 +24,7 @@ void main() async { FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError; await Preferences.init(); await Preferences.migrate(); + await PasswordService.migrate(); await GeolocationService.init(); await PushService.init(); runApp(const MainApp()); diff --git a/lib/password_service.dart b/lib/password_service.dart index 508af32..540882b 100644 --- a/lib/password_service.dart +++ b/lib/password_service.dart @@ -1,13 +1,22 @@ import 'package:flutter/material.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:traccar_client/l10n/app_localizations.dart'; +import 'package:traccar_client/preferences.dart'; class PasswordService { static final FlutterSecureStorage _secureStorage = const FlutterSecureStorage(); static const String _passwordKey = 'password'; + static Future migrate() async { + final oldPassword = await _secureStorage.read(key: _passwordKey); + if (oldPassword == null) return; + await Preferences.instance.setString(_passwordKey, oldPassword); + await _secureStorage.delete(key: _passwordKey); + } + static Future authenticate(BuildContext context) async { - if (!await _secureStorage.containsKey(key: _passwordKey)) return true; + final storedPassword = Preferences.instance.getString(_passwordKey); + if (storedPassword == null || storedPassword.isEmpty) return true; final controller = TextEditingController(); bool? result; if (context.mounted) { @@ -28,9 +37,8 @@ class PasswordService { ), TextButton( onPressed: () async { - final password = await _secureStorage.read(key: _passwordKey); if (context.mounted) { - Navigator.pop(context, password == controller.text); + Navigator.pop(context, storedPassword == controller.text); } }, child: Text(AppLocalizations.of(context)!.okButton), @@ -50,9 +58,9 @@ class PasswordService { static Future setPassword(String password) async { if (password.isNotEmpty) { - await _secureStorage.write(key: _passwordKey, value: password); + await Preferences.instance.setString(_passwordKey, password); } else { - await _secureStorage.delete(key: _passwordKey); + await Preferences.instance.remove(_passwordKey); } } } diff --git a/lib/preferences.dart b/lib/preferences.dart index fe206ab..3ec018d 100644 --- a/lib/preferences.dart +++ b/lib/preferences.dart @@ -37,7 +37,7 @@ class Preferences { fastestInterval, buffer, wakelock, stopDetection, lastTimestamp, lastLatitude, lastLongitude, lastHeading, 'device_id_preference', 'server_url_preference', 'accuracy_preference', - 'frequency_preference', 'distance_preference', 'buffer_preference', + 'frequency_preference', 'distance_preference', 'buffer_preference', 'password', }, ), );