Will unsubscribe the storageservice subscription if a new user is logged in

parent df49e3fd
import { isPlatformBrowser, isPlatformServer } from '@angular/common'; import { isPlatformBrowser, isPlatformServer } from '@angular/common';
import { Inject, Injectable, PLATFORM_ID } from '@angular/core'; import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
import { JwtHelperService } from '@auth0/angular-jwt'; import { JwtHelperService } from '@auth0/angular-jwt';
import { ReplaySubject } from 'rxjs'; import { ReplaySubject, Subject } from 'rxjs';
import { filter } from 'rxjs/operators'; import { filter, takeUntil } from 'rxjs/operators';
import { QuizEntity } from '../../lib/entities/QuizEntity'; import { QuizEntity } from '../../lib/entities/QuizEntity';
import { DbState, StorageKey } from '../../lib/enums/enums'; import { DbState, StorageKey } from '../../lib/enums/enums';
import { StatusProtocol } from '../../lib/enums/Message'; import { StatusProtocol } from '../../lib/enums/Message';
...@@ -16,12 +16,6 @@ import { StorageService } from '../storage/storage.service'; ...@@ -16,12 +16,6 @@ import { StorageService } from '../storage/storage.service';
providedIn: 'root', providedIn: 'root',
}) })
export class UserService { export class UserService {
private _isLoggedIn: boolean;
get isLoggedIn(): boolean {
return this._isLoggedIn;
}
set isLoggedIn(value: boolean) { set isLoggedIn(value: boolean) {
if (!value) { if (!value) {
this._casTicket = null; this._casTicket = null;
...@@ -36,7 +30,8 @@ export class UserService { ...@@ -36,7 +30,8 @@ export class UserService {
} }
if (isPlatformBrowser(this.platformId)) { if (isPlatformBrowser(this.platformId)) {
console.log(`UserService: switching db to user ${this.username} - isLoggedIn: ${value}`); console.log(`UserService: switching db to user ${this.username} - isLoggedIn: ${value}`);
this.storageService.switchDb(this._username).subscribe(() => { this._destroy.next();
this.storageService.switchDb(this._username).pipe(takeUntil(this._destroy)).subscribe(() => {
this._isLoggedIn = value; this._isLoggedIn = value;
this._loginNotifier.next(value); this._loginNotifier.next(value);
}); });
...@@ -46,6 +41,14 @@ export class UserService { ...@@ -46,6 +41,14 @@ export class UserService {
} }
} }
private _isLoggedIn: boolean;
get isLoggedIn(): boolean {
return this._isLoggedIn;
}
private readonly _destroy = new Subject();
private _staticLoginTokenContent: ILoginSerialized; private _staticLoginTokenContent: ILoginSerialized;
get staticLoginTokenContent(): ILoginSerialized { get staticLoginTokenContent(): ILoginSerialized {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment