import { Component , OnInit } from '@angular/core' ; import { HttpClient } from '@angular/common/http' ; import { Router } from "@angular/router" ; import { Observable } from 'rxjs/Observable' ; import { tap , catchError } from 'rxjs/operators' ; import { of } from 'rxjs/observable/of' ; @ Component ( { selector : 'app-login' , templateUrl : './login.component.html' , styleUrls : [ './login.component.css' ] } ) export class LoginComponent implements OnInit { loginData = { username : '' , password : '' } ; message = '' ; data : any ; constructor ( private http , private router ) { } ngOnInit ( ) { } login ( ) { this . http . post ( '/api/signin' , this . loginData ) . subscribe ( resp => { this . data = resp ; localStorage . setItem ( 'jwtToken' , this . data . token ) ; this . router . navigate ( [ 'books' ] ) ; } , err => { this . message = err . error . msg ; } ) ; } private handleError < T > ( operation = 'operation' , result ? ) { return ( error ) : Observable < T > => { console . error ( error ) ; console . log ( `${ operation } failed: ${ error . message }` ) ; return of ( result as T ) ; } ; } }