From 96200e04814f4eb332538ee5b2216264c314bb9f Mon Sep 17 00:00:00 2001 From: cogwheel0 <172976095+cogwheel0@users.noreply.github.com> Date: Tue, 9 Sep 2025 13:00:47 +0530 Subject: [PATCH] fix: credentials not persisting on some devices --- android/app/src/main/AndroidManifest.xml | 1 + lib/core/providers/app_providers.dart | 15 ++++++++++++++- lib/core/services/optimized_storage_service.dart | 2 +- lib/core/services/secure_credential_storage.dart | 10 ++++------ lib/core/services/storage_service.dart | 2 +- lib/main.dart | 13 ++++++++++++- 6 files changed, 33 insertions(+), 10 deletions(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 6e254cb..f19a4e2 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -22,6 +22,7 @@ android:name="${applicationName}" android:icon="@mipmap/ic_launcher" android:allowBackup="false" + android:fullBackupContent="false" android:usesCleartextTraffic="true"> ((ref) { }); final secureStorageProvider = Provider((ref) { - return const FlutterSecureStorage(); + // Single, shared instance with explicit platform options + return const FlutterSecureStorage( + aOptions: AndroidOptions( + encryptedSharedPreferences: true, + sharedPreferencesName: 'conduit_secure_prefs', + preferencesKeyPrefix: 'conduit_', + // Avoid auto-wipe on transient errors; we handle errors in code + resetOnError: false, + ), + iOptions: IOSOptions( + accountName: 'conduit_secure_storage', + synchronizable: false, + ), + ); }); final storageServiceProvider = Provider((ref) { diff --git a/lib/core/services/optimized_storage_service.dart b/lib/core/services/optimized_storage_service.dart index a06db1c..8727f0e 100644 --- a/lib/core/services/optimized_storage_service.dart +++ b/lib/core/services/optimized_storage_service.dart @@ -16,7 +16,7 @@ class OptimizedStorageService { required FlutterSecureStorage secureStorage, required SharedPreferences prefs, }) : _prefs = prefs, - _secureCredentialStorage = SecureCredentialStorage(); + _secureCredentialStorage = SecureCredentialStorage(instance: secureStorage); // Optimized key names with versioning static const String _authTokenKey = 'auth_token_v3'; diff --git a/lib/core/services/secure_credential_storage.dart b/lib/core/services/secure_credential_storage.dart index 799e9d9..8536653 100644 --- a/lib/core/services/secure_credential_storage.dart +++ b/lib/core/services/secure_credential_storage.dart @@ -8,8 +8,8 @@ import '../utils/debug_logger.dart'; class SecureCredentialStorage { late final FlutterSecureStorage _secureStorage; - SecureCredentialStorage() { - _secureStorage = FlutterSecureStorage( + SecureCredentialStorage({FlutterSecureStorage? instance}) { + _secureStorage = instance ?? FlutterSecureStorage( aOptions: _getAndroidOptions(), iOptions: _getIOSOptions(), ); @@ -25,10 +25,8 @@ class SecureCredentialStorage { encryptedSharedPreferences: true, sharedPreferencesName: 'conduit_secure_prefs', preferencesKeyPrefix: 'conduit_', - resetOnError: true, - // Use more compatible encryption algorithms - keyCipherAlgorithm: KeyCipherAlgorithm.RSA_ECB_PKCS1Padding, - storageCipherAlgorithm: StorageCipherAlgorithm.AES_CBC_PKCS7Padding, + // Avoid auto-wipe on transient errors; handle gracefully in code + resetOnError: false, ); } diff --git a/lib/core/services/storage_service.dart b/lib/core/services/storage_service.dart index f0da75b..b0bff3e 100644 --- a/lib/core/services/storage_service.dart +++ b/lib/core/services/storage_service.dart @@ -16,7 +16,7 @@ class StorageService { required SharedPreferences prefs, }) : _secureStorage = secureStorage, _prefs = prefs, - _secureCredentialStorage = SecureCredentialStorage(); + _secureCredentialStorage = SecureCredentialStorage(instance: secureStorage); // Secure storage keys static const String _authTokenKey = 'auth_token'; diff --git a/lib/main.dart b/lib/main.dart index c184ea5..4931487 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -29,7 +29,18 @@ void main() async { await SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge); final sharedPrefs = await SharedPreferences.getInstance(); - const secureStorage = FlutterSecureStorage(); + const secureStorage = FlutterSecureStorage( + aOptions: AndroidOptions( + encryptedSharedPreferences: true, + sharedPreferencesName: 'conduit_secure_prefs', + preferencesKeyPrefix: 'conduit_', + resetOnError: false, + ), + iOptions: IOSOptions( + accountName: 'conduit_secure_storage', + synchronizable: false, + ), + ); runApp( ProviderScope(