Skip to content

Commit

Permalink
fix(android): Clear secure storage on exception
Browse files Browse the repository at this point in the history
  • Loading branch information
kbn committed Dec 16, 2022
1 parent 9f85714 commit ffa32d1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
3 changes: 3 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<activity
android:name=".MainActivity"
Expand Down
4 changes: 4 additions & 0 deletions android/app/src/main/res/xml/backup_rules.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
<exclude domain="sharedpref" path="FlutterSecureStorage"/>
</full-backup-content>
7 changes: 7 additions & 0 deletions android/app/src/main/res/xml/data_extraction_rules.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<data-extraction-rules>
<cloud-backup>
<include domain="sharedpref" path="."/>
<exclude domain="sharedpref" path="FlutterSecureStorage"/>
</cloud-backup>
</data-extraction-rules>
20 changes: 14 additions & 6 deletions lib/viewmodels/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -70,9 +68,19 @@ class AuthModel extends ChangeNotifier {
return this;
}

Future<String?> readStorage(String key) async {
try {
return await storage.read(key: key);
}
catch(e) {
await storage.deleteAll();
return null;
}
}

Future<String> get token async {
if (_token == '') {
_token = (await storage.read(key: 'token')) ?? '';
_token = (await readStorage('token')) ?? '';
}
return _token;
}
Expand Down Expand Up @@ -156,7 +164,7 @@ class AuthModel extends ChangeNotifier {
}

Future<String> get lastUsername async {
return await storage.read(key: 'lastUsername') ?? '';
return await readStorage('lastUsername') ?? '';
}

Future<List<Lesson>> getUpcomingLessons({required int page, required int perPage}) async {
Expand Down

0 comments on commit ffa32d1

Please sign in to comment.