| .. | .. |
|---|
| 1 | 1 | import { LocaleService } from './common/i18n'; |
|---|
| 2 | +import { BasicService } from './common/utils'; |
|---|
| 2 | 3 | import { Injectable } from '@angular/core'; |
|---|
| 3 | 4 | import { Router } from '@angular/router'; |
|---|
| 4 | 5 | import { Location } from '@angular/common'; |
|---|
| .. | .. |
|---|
| 10 | 11 | const SECURIS_TOKEN = "X-SECURIS-TOKEN"; |
|---|
| 11 | 12 | |
|---|
| 12 | 13 | @Injectable() |
|---|
| 13 | | -export class UserService { |
|---|
| 14 | +export class UserService extends BasicService { |
|---|
| 14 | 15 | |
|---|
| 15 | 16 | count : number = 0; |
|---|
| 16 | 17 | |
|---|
| 17 | | - constructor(private $L: LocaleService, |
|---|
| 18 | + constructor($L: LocaleService, |
|---|
| 18 | 19 | private router: Router, |
|---|
| 19 | 20 | private store: LocalStorageService, |
|---|
| 20 | 21 | private http: Http) { |
|---|
| 21 | | - |
|---|
| 22 | + super($L); |
|---|
| 22 | 23 | } |
|---|
| 23 | 24 | |
|---|
| 24 | 25 | public login(username: string, password: string) : Observable<string> { |
|---|
| .. | .. |
|---|
| 28 | 29 | let options = new RequestOptions({ headers: new Headers({ "Content-Type": "application/x-www-form-urlencoded" })}); |
|---|
| 29 | 30 | return this.http.post('user/login', params.toString(), options) |
|---|
| 30 | 31 | .map((resp) => this.mapLogin(resp)) |
|---|
| 31 | | - .catch((err) => this.handleError(err)); |
|---|
| 32 | + .catch((err) => super.processErrorResponse(err)); |
|---|
| 32 | 33 | } |
|---|
| 33 | 34 | |
|---|
| 34 | 35 | private mapLogin(res : Response) : string { |
|---|
| .. | .. |
|---|
| 36 | 37 | this.store.set('user_full_name', data.full_name); |
|---|
| 37 | 38 | this.store.set('username', data.username); |
|---|
| 38 | 39 | this.store.set('token', data.token); |
|---|
| 40 | + console.log('New login token: ' + data.token); |
|---|
| 39 | 41 | return <string>data.token; |
|---|
| 40 | 42 | } |
|---|
| 41 | 43 | |
|---|
| .. | .. |
|---|
| 47 | 49 | let option = new RequestOptions({ headers: new Headers({ 'X-SECURIS-TOKEN': token }) }); |
|---|
| 48 | 50 | return this.http.get('check', option) |
|---|
| 49 | 51 | .map((resp) => this.mapCheck(resp)) |
|---|
| 50 | | - .catch((err) => this.handleError(err)); |
|---|
| 52 | + .catch((err) => super.processErrorResponse(err)); |
|---|
| 51 | 53 | } |
|---|
| 52 | 54 | |
|---|
| 53 | 55 | private mapCheck(res : Response) : boolean { |
|---|
| .. | .. |
|---|
| 67 | 69 | this.router.navigate(['public/login']); |
|---|
| 68 | 70 | } |
|---|
| 69 | 71 | |
|---|
| 70 | | - |
|---|
| 71 | | - private handleError (error: Response | any) { |
|---|
| 72 | | - // In a real world app, we might use a remote logging infrastructure |
|---|
| 73 | | - let errMsg: string; |
|---|
| 74 | | - if (error instanceof Response) { |
|---|
| 75 | | - const err = JSON.stringify(error); |
|---|
| 76 | | - errMsg = `${error.status} - ${error.statusText || ''} ${err}`; |
|---|
| 77 | | - } else { |
|---|
| 78 | | - errMsg = error.message ? error.message : error.toString(); |
|---|
| 79 | | - } |
|---|
| 80 | | - |
|---|
| 81 | | - if (error.status === 403 /* forbidden */ || error.status === 401 /* unauthorized */) { |
|---|
| 82 | | - errMsg = this.$L.get('Invalid credentials'); |
|---|
| 83 | | - } else if (error.status === 418 /* Teapot */) { |
|---|
| 84 | | - errMsg = this.$L.get(error.headers.get('X-SECURIS-ERROR-MSG')); |
|---|
| 85 | | - } else { |
|---|
| 86 | | - console.error(error); |
|---|
| 87 | | - errMsg = this.$L.get(`Unexpected error HTTP (${error.status}) accessing to server. Contact with the administrator.`); |
|---|
| 88 | | - } |
|---|
| 89 | | - |
|---|
| 90 | | - console.error(errMsg); |
|---|
| 91 | | - return Observable.throw(errMsg); |
|---|
| 92 | | - } |
|---|
| 93 | 72 | |
|---|
| 94 | 73 | } |
|---|
| 95 | 74 | |
|---|