From 45a23eff88ff36f13e28154e121bf7258339d64c Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Wed, 17 Mar 2021 14:14:30 +0100 Subject: [PATCH] MOBILE-3645 login: Fix consecutive logins if logout+SSO --- src/core/services/database/sites.ts | 6 +++--- src/core/services/sites.ts | 17 +++++++++++------ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/core/services/database/sites.ts b/src/core/services/database/sites.ts index a6eea9464..653db3620 100644 --- a/src/core/services/database/sites.ts +++ b/src/core/services/database/sites.ts @@ -211,11 +211,11 @@ export type SiteDBEntry = { id: string; siteUrl: string; token: string; - info: string; + info?: string | null; privateToken: string; - config: string; + config?: string | null; loggedOut: number; - oauthId: number; + oauthId?: number | null; }; export type CurrentSiteDBEntry = { diff --git a/src/core/services/sites.ts b/src/core/services/sites.ts index 801d0a114..b1bfd4dd3 100644 --- a/src/core/services/sites.ts +++ b/src/core/services/sites.ts @@ -688,7 +688,7 @@ export class CoreSitesProvider { oauthId?: number, ): Promise { const db = await this.appDB; - const entry = { + const entry: SiteDBEntry = { id, siteUrl, token, @@ -1004,7 +1004,7 @@ export class CoreSitesProvider { const config = entry.config ? CoreTextUtils.parseJSON(entry.config) : undefined; const site = new CoreSite(entry.id, entry.siteUrl, entry.token, info, entry.privateToken, config, entry.loggedOut == 1); - site.setOAuthId(entry.oauthId); + site.setOAuthId(entry.oauthId || undefined); return this.migrateSiteSchemas(site).then(() => { // Set site after migrating schemas, or a call to getSite could get the site while tables are being created. @@ -1221,11 +1221,16 @@ export class CoreSitesProvider { async setSiteLoggedOut(siteId: string, loggedOut: boolean): Promise { const db = await this.appDB; const site = await this.getSite(siteId); - const newValues = { - token: '', // Erase the token for security. + const newValues: Partial = { loggedOut: loggedOut ? 1 : 0, }; + if (loggedOut) { + // Erase the token for security. + newValues.token = ''; + site.token = ''; + } + site.setLoggedOut(loggedOut); await db.updateRecords(SITES_TABLE_NAME, newValues, { id: siteId }); @@ -1266,7 +1271,7 @@ export class CoreSitesProvider { async updateSiteTokenBySiteId(siteId: string, token: string, privateToken: string = ''): Promise { const db = await this.appDB; const site = await this.getSite(siteId); - const newValues = { + const newValues: Partial = { token, privateToken, loggedOut: 0, @@ -1307,7 +1312,7 @@ export class CoreSitesProvider { // Error getting config, keep the current one. } - const newValues: Record = { + const newValues: Partial = { info: JSON.stringify(info), loggedOut: site.isLoggedOut() ? 1 : 0, };