mirror of
https://gitlab.com/famedly/fluffychat.git
synced 2024-11-24 04:59:26 +01:00
fix: SecureStorage sometimes reading wrong / bad values
This commit is contained in:
parent
b0d60c874a
commit
d94f0d7351
@ -40,9 +40,30 @@ Future<Database> getDatabase(Client client) async {
|
|||||||
Database _db;
|
Database _db;
|
||||||
bool _generateDatabaseLock = false;
|
bool _generateDatabaseLock = false;
|
||||||
|
|
||||||
|
// see https://github.com/mogol/flutter_secure_storage/issues/161#issuecomment-704578453
|
||||||
|
class AsyncMutex {
|
||||||
|
Completer<void> _completer;
|
||||||
|
|
||||||
|
Future<void> lock() async {
|
||||||
|
while (_completer != null) {
|
||||||
|
await _completer.future;
|
||||||
|
}
|
||||||
|
|
||||||
|
_completer = Completer<void>();
|
||||||
|
}
|
||||||
|
|
||||||
|
void unlock() {
|
||||||
|
assert(_completer != null);
|
||||||
|
final completer = _completer;
|
||||||
|
_completer = null;
|
||||||
|
completer.complete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class Store {
|
class Store {
|
||||||
LocalStorage storage;
|
LocalStorage storage;
|
||||||
final FlutterSecureStorage secureStorage;
|
final FlutterSecureStorage secureStorage;
|
||||||
|
static final _mutex = AsyncMutex();
|
||||||
|
|
||||||
Store()
|
Store()
|
||||||
: secureStorage = PlatformInfos.isMobile ? FlutterSecureStorage() : null;
|
: secureStorage = PlatformInfos.isMobile ? FlutterSecureStorage() : null;
|
||||||
@ -69,6 +90,7 @@ class Store {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
await _mutex.lock();
|
||||||
return await secureStorage.read(key: key);
|
return await secureStorage.read(key: key);
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
return null;
|
return null;
|
||||||
@ -89,6 +111,7 @@ class Store {
|
|||||||
await _setupLocalStorage();
|
await _setupLocalStorage();
|
||||||
return await storage.setItem(key, value);
|
return await storage.setItem(key, value);
|
||||||
}
|
}
|
||||||
|
await _mutex.lock();
|
||||||
return await secureStorage.write(key: key, value: value);
|
return await secureStorage.write(key: key, value: value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,6 +124,7 @@ class Store {
|
|||||||
await _setupLocalStorage();
|
await _setupLocalStorage();
|
||||||
return await storage.deleteItem(key);
|
return await storage.deleteItem(key);
|
||||||
}
|
}
|
||||||
|
await _mutex.lock();
|
||||||
return await secureStorage.delete(key: key);
|
return await secureStorage.delete(key: key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user