Skip to content

Commit

Permalink
retain account accross DB updates to avoid logout
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Jun 11, 2022
1 parent b499f36 commit 048a181
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions TinodiosDB/BaseDb.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,17 @@ public class BaseDb {
self.subscriberDb = SubscriberDb(self.db!, baseDb: self)
self.messageDb = MessageDb(self.db!, baseDb: self)

var account: StoredAccount? = nil
var deviceToken: String? = nil
if self.db!.schemaVersion != BaseDb.kSchemaVersion {
BaseDb.log.info("BaseDb - schema has changed from %d to %d",
(self.db?.schemaVersion ?? -1), BaseDb.kSchemaVersion)
// Delete database if schema has changed.
self.dropDb()
BaseDb.log.info("BaseDb - schema has changed from %d to %d", (self.db?.schemaVersion ?? -1), BaseDb.kSchemaVersion)

// Retain active account accross DB upgrades to keep user logged in.
account = self.accountDb!.getActiveAccount()
deviceToken = self.accountDb!.getDeviceToken()

// Schema has changed, delete database.
self.dropDb()
self.db!.schemaVersion = BaseDb.kSchemaVersion
}

Expand All @@ -109,8 +114,13 @@ public class BaseDb {
// Enable foreign key enforcement.
try! self.db!.run("PRAGMA foreign_keys = ON")

if let account = account {
_ = self.accountDb!.addOrActivateAccount(for: account.uid, withCredMethods: account.credMethods)
self.accountDb!.saveDeviceToken(token: deviceToken)
}
self.account = self.accountDb!.getActiveAccount()
}

private func dropDb() {
self.messageDb?.destroyTable()
self.subscriberDb?.destroyTable()
Expand Down

0 comments on commit 048a181

Please sign in to comment.