import { Injectable } from '@angular/core' ; import { Http , Response , Headers } from '@angular/http' ; import { Observable } from 'rxjs' ; import { Configuration } from '../app.constants' ; import 'rxjs/add/operator/map' ; @ Injectable ( ) export class AuthenticationService { public actionUrl : string ; public token : string ; private TOKEN_KEY = 'token' ; private USER_KEY : string = 'currentUser' ; public user : any ; public constructor ( private _http , private _configuration ) { this . actionUrl = `${ _configuration . apiHost }${ _configuration . apiPrefix }login` ; this . token = this . getToken ( ) ; } public login ( username , password ) { return this . _http . post ( this . actionUrl , { username : username , password : password } ) . map ( ( response ) => { if ( ! response || ! response . json || ! response . json ( ) ) { return false ; } let user = response . json ( ) . user ; let token = response . json ( ) . token ; if ( ! token ) { return false ; } this . token = token ; localStorage . setItem ( this . TOKEN_KEY , JSON . stringify ( { token } ) ) ; localStorage . setItem ( this . USER_KEY , JSON . stringify ( { user } ) ) ; return true ; } ) . catch ( ( error ) => Observable . throw ( error . json ( ) . error || 'Server error' ) ) ; } public logout ( ) { this . token = null ; localStorage . removeItem ( this . USER_KEY ) ; localStorage . removeItem ( this . TOKEN_KEY ) ; } public createAuthorizationHeader ( ) { let headers = new Headers ( ) ; headers . append ( 'x-access-token' , this . getToken ( ) ) ; headers . append ( 'Content-Type' , 'application/json' ) ; return headers ; } private getToken ( ) { let userToken = JSON . parse ( localStorage . getItem ( this . TOKEN_KEY ) ) ; return userToken ? userToken . token : null ; } }