Created
March 28, 2019 15:05
-
-
Save 4skinSkywalker/ea1dff83273a1cab22ec6d5fed5640cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Injectable } from '@angular/core' | |
| import { HttpClient, HttpErrorResponse } from '@angular/common/http' | |
| import { tap, catchError } from 'rxjs/operators' | |
| import { throwError } from 'rxjs' | |
| @Injectable({ | |
| providedIn: 'root' | |
| }) | |
| export class AuthService { | |
| constructor(private http: HttpClient) { } | |
| login(credentials) { | |
| return this.http.post('/api/authentication', credentials) | |
| .pipe( | |
| catchError(this.handleError), | |
| tap(response => { | |
| localStorage.setItem('token', response['token']) | |
| }) | |
| ) | |
| } | |
| private handleError(error: HttpErrorResponse) { | |
| if (error.error instanceof ErrorEvent) { | |
| // Client-side or network error | |
| } else { | |
| // Unsuccessful response code | |
| if (error.status === 401) { | |
| return throwError( 'Unauthorized.' ) | |
| } | |
| } | |
| return throwError( error.error ) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment