/**
 * Sessions
 */ import { EventEmitter , Injectable } from '@angular/core' ; import { ConfigsService } from '../common/services/configs.service' ; import { Storage } from './storage' ; import { BehaviorSubject } from 'rxjs' ; import { MindsUser } from '../interfaces/entities' ; @ Injectable ( ) export class Session { loggedinEmitter : EventEmitter < any > = new EventEmitter ( ) ; userEmitter : EventEmitter < any > = new EventEmitter ( ) ; readonly user$ : BehaviorSubject < MindsUser | null > = new BehaviorSubject < MindsUser | null > ( null ) ; constructor ( private configs , private storage ) { this . user$ . next ( this . configs . get ( 'user' ) || null ) ; } /**
     * Return if loggedin, with an optional listener
     */ isLoggedIn ( observe = null ) { if ( observe ) { this . loggedinEmitter . subscribe ( { next : is => { if ( is ) observe ( true ) ; else observe ( false ) ; } , } ) ; } if ( this . configs . get ( 'LoggedIn' ) ) return true ; return false ; } isAdmin ( ) { if ( ! this . isLoggedIn ) return false ; if ( this . configs . get ( 'Admin' ) ) return true ; return false ; } /**
     * Get the loggedin user
     */ getLoggedInUser ( observe = null ) { if ( observe ) { this . userEmitter . subscribe ( { next : user => { observe ( user ) ; } , } ) ; } const user = this . configs . get ( 'user' ) ; if ( user ) { return user ; } return false ; } inject ( user = null ) { this . storage . clear ( ) ; this . userEmitter . next ( user ) ; this . user$ . next ( user ? ( user as MindsUser ) : null ) ; this . configs . set ( 'LoggedIn' , true ) ; this . configs . set ( 'user' , user ) ; if ( user . admin === true ) { this . configs . set ( 'Admin' , true ) ; } } /**
     * Inject user and emit login event
     */ login ( user = null ) { this . inject ( user ) ; this . loggedinEmitter . next ( true ) ; } /**
     * Emit logout event
     */ logout ( ) { this . userEmitter . next ( null ) ; this . user$ . next ( null ) ; this . configs . set ( 'user' , null ) ; this . configs . set ( 'LoggedIn' , false ) ; this . configs . set ( 'Admin' , false ) ; this . storage . clear ( ) ; this . loggedinEmitter . next ( false ) ; } }