1
|
import {Injectable} from '@angular/core';
|
2
|
import {BehaviorSubject, Observable} from 'rxjs';
|
3
|
import {User} from '../models/user';
|
4
|
|
5
|
@Injectable({
|
6
|
providedIn: 'root'
|
7
|
})
|
8
|
export class UserState {
|
9
|
private userState$: BehaviorSubject<User> = new BehaviorSubject<User>(null);
|
10
|
|
11
|
constructor() {}
|
12
|
|
13
|
setUser(user: User): void {
|
14
|
this.userState$.next(user);
|
15
|
}
|
16
|
|
17
|
setLoggedIn(loggedIn: boolean) {
|
18
|
this.userState$.next({...this.userState$.getValue(), isLoggedIn: loggedIn})
|
19
|
}
|
20
|
|
21
|
getUser(): User {
|
22
|
return this.userState$.getValue();
|
23
|
}
|
24
|
|
25
|
getUser$(refresh: boolean = false): Observable<User> {
|
26
|
return this.userState$.asObservable();
|
27
|
}
|
28
|
|
29
|
getLoggedIn(): boolean {
|
30
|
return this.userState$.getValue().isLoggedIn;
|
31
|
}
|
32
|
}
|