| .. | .. |
|---|
| 1 | +import { LocaleService } from './common/i18n'; |
|---|
| 1 | 2 | import { Injectable } from '@angular/core'; |
|---|
| 2 | 3 | import { Router } from '@angular/router'; |
|---|
| 3 | 4 | import { Location } from '@angular/common'; |
|---|
| .. | .. |
|---|
| 11 | 12 | @Injectable() |
|---|
| 12 | 13 | export class UserService { |
|---|
| 13 | 14 | |
|---|
| 14 | | - constructor(private router: Router, |
|---|
| 15 | + constructor(private $L: LocaleService, |
|---|
| 16 | + private router: Router, |
|---|
| 15 | 17 | private store: LocalStorageService, |
|---|
| 16 | 18 | private http: Http) { |
|---|
| 17 | 19 | |
|---|
| .. | .. |
|---|
| 22 | 24 | params.append('username', username); |
|---|
| 23 | 25 | params.append('password', password); |
|---|
| 24 | 26 | let options = new RequestOptions({ headers: new Headers({ "Content-Type": "application/x-www-form-urlencoded" })}); |
|---|
| 25 | | - return this.http.post('user/login', params, options) |
|---|
| 27 | + return this.http.post('user/login', params.toString(), options) |
|---|
| 26 | 28 | .map((res: Response) => { |
|---|
| 27 | 29 | let data = res.json(); |
|---|
| 28 | 30 | this.store.set('username', username); |
|---|
| 29 | 31 | this.store.set('token', data.token); |
|---|
| 30 | 32 | return <string>data.token; |
|---|
| 31 | 33 | }) |
|---|
| 32 | | - .catch(this.handleError); |
|---|
| 34 | + .catch((r) => this.handleError(r)); |
|---|
| 33 | 35 | } |
|---|
| 34 | 36 | |
|---|
| 35 | 37 | isLoggedIn() : Observable<Boolean> { |
|---|
| .. | .. |
|---|
| 45 | 47 | } |
|---|
| 46 | 48 | return body.valid; |
|---|
| 47 | 49 | }) |
|---|
| 48 | | - .catch(this.handleError) |
|---|
| 50 | + .catch((r) => this.handleError(r)) |
|---|
| 49 | 51 | .catch(() => Observable.of(false)); |
|---|
| 50 | 52 | } |
|---|
| 51 | 53 | |
|---|
| .. | .. |
|---|
| 62 | 64 | // In a real world app, we might use a remote logging infrastructure |
|---|
| 63 | 65 | let errMsg: string; |
|---|
| 64 | 66 | if (error instanceof Response) { |
|---|
| 65 | | - const body = error.json() || ''; |
|---|
| 66 | | - const err = body.error || JSON.stringify(body); |
|---|
| 67 | + const err = JSON.stringify(error); |
|---|
| 67 | 68 | errMsg = `${error.status} - ${error.statusText || ''} ${err}`; |
|---|
| 68 | 69 | } else { |
|---|
| 69 | 70 | errMsg = error.message ? error.message : error.toString(); |
|---|
| 70 | 71 | } |
|---|
| 72 | + |
|---|
| 73 | + if (error.status === 403 /* forbidden */ || error.status === 401 /* unauthorized */) { |
|---|
| 74 | + errMsg = this.$L.get('Invalid credentials'); |
|---|
| 75 | + } else if (error.status === 418 /* Teapot */) { |
|---|
| 76 | + errMsg = this.$L.get(error.headers.get('X-SECURIS-ERROR-MSG')); |
|---|
| 77 | + } else { |
|---|
| 78 | + console.error(error); |
|---|
| 79 | + errMsg = this.$L.get(`Unexpected error HTTP (${error.status}) accessing to server. Contact with the administrator.`); |
|---|
| 80 | + } |
|---|
| 81 | + |
|---|
| 71 | 82 | console.error(errMsg); |
|---|
| 72 | 83 | return Observable.throw(errMsg); |
|---|
| 73 | 84 | } |
|---|