import { Injectable } from '@angular/core' ; import { HttpClient } from '@angular/common/http' ; import { BehaviorSubject , Observable } from 'rxjs' ; import { map } from 'rxjs/operators' ; import { environment } from '@environments/environment' ; import { User } from '@app/_models' ; @ Injectable ( { providedIn : 'root' } ) export class AuthenticationService { private currentUserSubject : BehaviorSubject < User > ; public currentUser : Observable < User > ; constructor ( private http ) { this . currentUserSubject = new BehaviorSubject < User > ( JSON . parse ( localStorage . getItem ( 'currentUser' ) ) ) ; this . currentUser = this . currentUserSubject . asObservable ( ) ; } public get currentUserValue ( ) : User { return this . currentUserSubject . value ; } login ( username , password ) { return this . http . post < any > ( `${ environment . apiUrl }/users/authenticate` , { username , password } ) . pipe ( map ( user => { if ( user && user . token ) { localStorage . setItem ( 'currentUser' , JSON . stringify ( user ) ) ; this . currentUserSubject . next ( user ) ; } return user ; } ) ) ; } logout ( ) { localStorage . removeItem ( 'currentUser' ) ; this . currentUserSubject . next ( null ) ; } }