From abf4afe76e14d2f8b91a37f72b3858c3334b6e02 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Tue, 4 Apr 2023 16:06:51 +0530 Subject: [PATCH] Fix: Remove synced codes on logout --- lib/models/authenticator/local_auth_entity.dart | 3 +++ lib/services/authenticator_service.dart | 4 ++++ lib/store/authenticator_db.dart | 7 +++++++ 3 files changed, 14 insertions(+) diff --git a/lib/models/authenticator/local_auth_entity.dart b/lib/models/authenticator/local_auth_entity.dart index 2b1535e66..373953d54 100644 --- a/lib/models/authenticator/local_auth_entity.dart +++ b/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; diff --git a/lib/services/authenticator_service.dart b/lib/services/authenticator_service.dart index 671418f65..93a58718b 100644 --- a/lib/services/authenticator_service.dart +++ b/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); diff --git a/lib/store/authenticator_db.dart b/lib/store/authenticator_db.dart index 97852b364..f1f653213 100644 --- a/lib/store/authenticator_db.dart +++ b/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 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 deleteByIDs({List? generatedIDs, List? ids}) async { final db = await instance.database;