declare var Object ; import { Injectable , Inject } from '@angular/core' ; import { InternalStorage } from '../../storage/storage.swaps' ; import { SDKToken } from '../../models/BaseModels' ; author module license description @ Injectable ( ) export class LoopBackAuth { type { SDKToken } private token : SDKToken = new SDKToken ( ) ; type { string } protected prefix : string = '$LoopBackSDK$' ; method param { InternalStorage } storage description constructor ( @ Inject ( InternalStorage ) protected storage ) { this . token . id = this . load ( 'id' ) ; this . token . user = this . load ( 'user' ) ; this . token . userId = this . load ( 'userId' ) ; this . token . issuedAt = this . load ( 'issuedAt' ) ; this . token . created = this . load ( 'created' ) ; this . token . ttl = this . load ( 'ttl' ) ; this . token . rememberMe = this . load ( 'rememberMe' ) ; } method param { boolean } value return { void } description public setRememberMe ( value ) { this . token . rememberMe = value ; } method param { any } user return { void } description public setUser ( user ) { this . token . user = user ; this . save ( ) ; } method param { SDKToken } token return { void } description public setToken ( token ) { this . token = Object . assign ( this . token , token ) ; this . save ( ) ; } method return { void } description public getToken ( ) { return < SDKToken > this . token ; } method return { string } description public getAccessTokenId ( ) { return this . token . id ; } method return { any } description public getCurrentUserId ( ) { return this . token . userId ; } method return { any } description public getCurrentUserData ( ) { return ( typeof this . token . user === 'string' ) ? JSON . parse ( this . token . user ) : this . token . user ; } method return { boolean } description public save ( ) { if ( this . token . rememberMe ) { this . persist ( 'id' , this . token . id ) ; this . persist ( 'user' , this . token . user ) ; this . persist ( 'userId' , this . token . userId ) ; this . persist ( 'issuedAt' , this . token . issuedAt ) ; this . persist ( 'created' , this . token . created ) ; this . persist ( 'ttl' , this . token . ttl ) ; this . persist ( 'rememberMe' , this . token . rememberMe ) ; return true ; } else { return false ; } } ; method param { string } prop return { any } description protected load ( prop ) { return this . storage . get ( `${ this . prefix }${ prop }` ) ; } method return { void } description public clear ( ) { Object . keys ( this . token ) . forEach ( ( prop ) => this . storage . remove ( `${ this . prefix }${ prop }` ) ) ; this . token = new SDKToken ( ) ; } method return { void } description protected persist ( prop , value ) { try { this . storage . set ( `${ this . prefix }${ prop }` , ( typeof value === 'object' ) ? JSON . stringify ( value ) : value ) ; } catch ( err ) { console . error ( 'Cannot access local/session storage:' , err ) ; } } }