fix: credentials not persisting on some devices

This commit is contained in:
cogwheel0
2025-09-09 13:00:47 +05:30
parent f88ffcc548
commit 96200e0481
6 changed files with 33 additions and 10 deletions

View File

@@ -22,6 +22,7 @@
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher"
android:allowBackup="false"
android:fullBackupContent="false"
android:usesCleartextTraffic="true">
<activity
android:name=".MainActivity"

View File

@@ -29,7 +29,20 @@ final sharedPreferencesProvider = Provider<SharedPreferences>((ref) {
});
final secureStorageProvider = Provider<FlutterSecureStorage>((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<StorageService>((ref) {

View File

@@ -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';

View File

@@ -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,
);
}

View File

@@ -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';

View File

@@ -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(