From ffa32d15a573334c3666f492ba71161b304c5f92 Mon Sep 17 00:00:00 2001 From: Kristian Berge Nessa Date: Fri, 16 Dec 2022 20:56:36 +0100 Subject: [PATCH] fix(android): Clear secure storage on exception See flutter_secure_storage issue: https://github.com/mogol/flutter_secure_storage/issues/43#issuecomment-1326487020 --- android/app/src/main/AndroidManifest.xml | 3 +++ android/app/src/main/res/xml/backup_rules.xml | 4 ++++ .../main/res/xml/data_extraction_rules.xml | 7 +++++++ lib/viewmodels/auth.dart | 20 +++++++++++++------ 4 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 android/app/src/main/res/xml/backup_rules.xml create mode 100644 android/app/src/main/res/xml/data_extraction_rules.xml diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index a6f5eb0..e320288 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -9,6 +9,9 @@ android:name="${applicationName}" android:label="Music'scool" android:icon="@mipmap/ic_launcher" + android:allowBackup="true" + android:fullBackupContent="@xml/backup_rules" + android:dataExtractionRules="@xml/data_extraction_rules" android:usesCleartextTraffic="true"> + + + diff --git a/android/app/src/main/res/xml/data_extraction_rules.xml b/android/app/src/main/res/xml/data_extraction_rules.xml new file mode 100644 index 0000000..196541c --- /dev/null +++ b/android/app/src/main/res/xml/data_extraction_rules.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/lib/viewmodels/auth.dart b/lib/viewmodels/auth.dart index b65aa05..f752488 100644 --- a/lib/viewmodels/auth.dart +++ b/lib/viewmodels/auth.dart @@ -42,11 +42,9 @@ class AuthModel extends ChangeNotifier { } else { print('token empty!'); } - value = await storage.read(key: 'notificationsEnabled'); - print('notificationsEnabled:${value}'); + value = await readStorage('notificationsEnabled'); if (value != null && value == 'false') _notificationsEnabled = false; - value = await storage.read(key: 'nextLesson'); - print('nextLesson:${value}'); + value = await readStorage('nextLesson'); if (value != null && value != '') { try { _nextLesson = DateTime.parse(value); @@ -70,9 +68,19 @@ class AuthModel extends ChangeNotifier { return this; } + Future readStorage(String key) async { + try { + return await storage.read(key: key); + } + catch(e) { + await storage.deleteAll(); + return null; + } + } + Future get token async { if (_token == '') { - _token = (await storage.read(key: 'token')) ?? ''; + _token = (await readStorage('token')) ?? ''; } return _token; } @@ -156,7 +164,7 @@ class AuthModel extends ChangeNotifier { } Future get lastUsername async { - return await storage.read(key: 'lastUsername') ?? ''; + return await readStorage('lastUsername') ?? ''; } Future> getUpcomingLessons({required int page, required int perPage}) async {