import { HttpClient } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; @ Injectable ( ) export class AuthService { public store : Storage ; public isLogged : boolean = true ; constructor ( private http ) { this . store = localStorage ; this . isLogged = true ; } get redirectUrl ( ) { return sessionStorage . getItem ( 'RedirectUrl' ) || '' ; } set redirectUrl ( url ) { sessionStorage . setItem ( 'RedirectUrl' , url ) ; } public login ( userInfo ) { this . http . post ( '/login' , userInfo ) . subscribe ( ( res ) => { this . isLogged = true ; this . store . setItem ( 'ACCESS_TOKEN' , res . msg ) ; this . store . setItem ( 'USER_INFO' , JSON . stringify ( res . data ) ) ; location . href = ! ! this . redirectUrl ? this . redirectUrl : '/dashboard' ; } , ( err ) => { } ) ; } public logout ( ) { this . http . post ( '/logout' , { } ) . subscribe ( ( res ) => { this . isLogged = false ; this . store . clear ( ) ; sessionStorage . clear ( ) ; setTimeout ( ( ) => { location . href = 'login' ; } , 1000 ) ; } , ( err ) => { } ) ; } }