Skip to content

Commit

Permalink
Fix [sc-5861]: Prevent bad account selection (#818)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mpellerin42 authored Jun 8, 2023
1 parent accb778 commit 19e4326
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
22 changes: 20 additions & 2 deletions apps/desktop/src/app/account/account.component.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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(['/']);
}
}
17 changes: 15 additions & 2 deletions apps/desktop/src/app/main-layout/main-layout.component.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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()) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 19e4326

Please sign in to comment.