From 19e4326f5b4aa6d91e1ce7831e973f080edf5bc3 Mon Sep 17 00:00:00 2001 From: Mat Pellerin Date: Thu, 8 Jun 2023 17:37:21 +0200 Subject: [PATCH] Fix [sc-5861]: Prevent bad account selection (#818) * Fix [sc-5861]: Prevent bad account selection In some corner cases happening only for people having several accounts on several environment (ie Stage and Prod), the Desktop ended up in a bad state where no account was selected. This PR is adding more checks and redirections to prevent this from happening. * Update Desktop version --- .../src/app/account/account.component.ts | 22 +++++++++++++++++-- .../app/main-layout/main-layout.component.ts | 17 ++++++++++++-- package.json | 2 +- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/apps/desktop/src/app/account/account.component.ts b/apps/desktop/src/app/account/account.component.ts index bf1a6fcf4..0f6206823 100644 --- a/apps/desktop/src/app/account/account.component.ts +++ b/apps/desktop/src/app/account/account.component.ts @@ -1,7 +1,8 @@ import { ChangeDetectionStrategy, Component } from '@angular/core'; import { Router } from '@angular/router'; -import { SyncService } from '../sync/sync.service'; +import { ACCOUNT_KEY, SyncService } from '../sync/sync.service'; import { SDKService } from '@flaps/core'; +import { catchError, of, tap } from 'rxjs'; @Component({ selector: 'nde-select-account', @@ -10,11 +11,28 @@ import { SDKService } from '@flaps/core'; changeDetection: ChangeDetectionStrategy.OnPush, }) export class SelectAccountComponent { - accounts = this.sdk.nuclia.db.getAccounts(); + accounts = this.sdk.nuclia.db.getAccounts().pipe( + tap((accounts) => { + if (accounts.length === 0) { + this.logout(); + } + }), + catchError(() => { + this.logout(); + return of([]); + }), + ); + constructor(private sdk: SDKService, private sync: SyncService, private router: Router) {} selectAccount(account: string) { this.sync.selectAccount(account); this.router.navigate(['/']); } + + logout() { + localStorage.removeItem(ACCOUNT_KEY); + this.sdk.nuclia.auth.logout(); + this.router.navigate(['/']); + } } diff --git a/apps/desktop/src/app/main-layout/main-layout.component.ts b/apps/desktop/src/app/main-layout/main-layout.component.ts index 7933e5147..945bb5b64 100644 --- a/apps/desktop/src/app/main-layout/main-layout.component.ts +++ b/apps/desktop/src/app/main-layout/main-layout.component.ts @@ -1,8 +1,8 @@ import { ChangeDetectionStrategy, ChangeDetectorRef, Component } from '@angular/core'; import { Router } from '@angular/router'; import { SDKService } from '@flaps/core'; -import { filter, take } from 'rxjs'; -import { SyncService } from '../sync/sync.service'; +import { catchError, filter, of, switchMap, take } from 'rxjs'; +import { ACCOUNT_KEY, SyncService } from '../sync/sync.service'; @Component({ selector: 'nde-main-layout', @@ -23,6 +23,19 @@ export class MainLayoutComponent { .pipe( filter((yes) => yes), take(1), + switchMap(() => { + const accountId = this.sync.getAccountId(); + return !accountId + ? of(false) + : this.sdk.nuclia.db.getKnowledgeBoxes(accountId).pipe( + catchError(() => { + localStorage.removeItem(ACCOUNT_KEY); + this.sdk.nuclia.auth.logout(); + this.router.navigate(['/']); + return of(false); + }), + ); + }), ) .subscribe(() => { if (!this.sync.getAccountId()) { diff --git a/package.json b/package.json index 7d8811402..5fdd52306 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nuclia", - "version": "2.4.0", + "version": "2.4.1", "license": "MIT", "author": "Nuclia.cloud", "description": "Nuclia frontend apps and libs",