From 19e75710c2bada39a99b782e81c700f1918aaa3a Mon Sep 17 00:00:00 2001 From: David Donges <david.donges@mni.thm.de> Date: Wed, 21 Mar 2018 00:24:46 +0100 Subject: [PATCH] Make user observable --- .../services/http/authentication.service.ts | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/app/services/http/authentication.service.ts b/src/app/services/http/authentication.service.ts index 3fce9041e..d0ed68091 100644 --- a/src/app/services/http/authentication.service.ts +++ b/src/app/services/http/authentication.service.ts @@ -6,11 +6,12 @@ import { UserRole } from '../../models/user-roles.enum'; import { DataStoreService } from '../util/data-store.service'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { ClientAuthentication } from '../../models/client-authentication'; +import { BehaviorSubject } from 'rxjs/BehaviorSubject'; @Injectable() export class AuthenticationService { private readonly STORAGE_KEY: string = 'USER'; - private user: User; + private user = new BehaviorSubject<User>(undefined); private apiUrl = { base: 'https://arsnova-staging.mni.thm.de/api', v2: 'https://arsnova-staging.mni.thm.de/api/v2', @@ -30,7 +31,7 @@ export class AuthenticationService { private http: HttpClient) { if (dataStoreService.has(this.STORAGE_KEY)) { // Load user data from local data store if available - this.user = JSON.parse(dataStoreService.get(this.STORAGE_KEY)); + this.user.next(JSON.parse(dataStoreService.get(this.STORAGE_KEY))); } } @@ -74,28 +75,28 @@ export class AuthenticationService { logout() { // Destroy the persisted user data this.dataStoreService.remove(this.STORAGE_KEY); - this.user = undefined; + this.user.next(undefined); } getUser(): User { - return this.user; + return this.user.getValue(); } private setUser(user: User): void { - this.user = user; - this.dataStoreService.set(this.STORAGE_KEY, JSON.stringify(this.user)); + this.user.next(user); + this.dataStoreService.set(this.STORAGE_KEY, JSON.stringify(user)); } isLoggedIn(): boolean { - return this.user !== undefined; + return this.user.getValue() !== undefined; } getRole(): UserRole { - return this.isLoggedIn() ? this.user.role : undefined; + return this.isLoggedIn() ? this.user.getValue().role : undefined; } getToken(): string { - return this.user.token; + return this.isLoggedIn() ? this.user.getValue().token : undefined; } private checkLogin(clientAuthentication: Observable<ClientAuthentication>, userRole: UserRole): Observable<boolean> { @@ -115,4 +116,7 @@ export class AuthenticationService { }); } + get watchUser() { + return this.user.asObservable(); + } } -- GitLab