Pārlūkot izejas kodu

Fix: Remove synced codes on logout

Neeraj Gupta 2 gadi atpakaļ
vecāks
revīzija
abf4afe76e

+ 3 - 0
lib/models/authenticator/local_auth_entity.dart

@@ -5,15 +5,18 @@ import 'package:flutter/material.dart';
 @immutable
 class LocalAuthEntity {
   final int generatedID;
+
   // id can be null if a code has been scanned locally but it's yet to be
   // synced with the remote server.
   final String? id;
   final String encryptedData;
   final String header;
+
   // createdAt and updateAt will be equal to local time of creation or updation
   // till remote sync is completed.
   final int createdAt;
   final int updatedAt;
+
   // shouldSync indicates that the entry was locally created or updated. The
   // app should try to sync it to the server during next sync
   final bool shouldSync;

+ 4 - 0
lib/services/authenticator_service.dart

@@ -134,7 +134,11 @@ class AuthenticatorService {
       _logger.info("local push completed");
       Bus.instance.fire(CodesUpdatedEvent());
     } on UnauthorizedError {
+      if ((await _db.removeSyncedData()) > 0) {
+        Bus.instance.fire(CodesUpdatedEvent());
+      }
       debugPrint("Firing logout event");
+
       Bus.instance.fire(TriggerLogoutEvent());
     } catch (e) {
       _logger.severe("Failed to sync with remote", e);

+ 7 - 0
lib/store/authenticator_db.dart

@@ -135,6 +135,13 @@ class AuthenticatorDB {
     return _convertRows(rows);
   }
 
+  // removeSyncedData will remove all the data which is synced with the server
+  Future<int> removeSyncedData() async {
+    final db = await instance.database;
+    return await db
+        .delete(entityTable, where: 'shouldSync = ?', whereArgs: [0]);
+  }
+
 // deleteByID will prefer generated id if both ids are passed during deletion
   Future<void> deleteByIDs({List<int>? generatedIDs, List<String>? ids}) async {
     final db = await instance.database;