rsanchez
2017-03-20 1a0491f2462d2c309bd8e310b22c11019a79ce1e
securis/src/main/webapp/src/app/user.service.ts
....@@ -1,4 +1,5 @@
11 import { LocaleService } from './common/i18n';
2
+import { BasicService } from './common/utils';
23 import { Injectable } from '@angular/core';
34 import { Router } from '@angular/router';
45 import { Location } from '@angular/common';
....@@ -10,15 +11,15 @@
1011 const SECURIS_TOKEN = "X-SECURIS-TOKEN";
1112
1213 @Injectable()
13
-export class UserService {
14
+export class UserService extends BasicService {
1415
1516 count : number = 0;
1617
17
- constructor(private $L: LocaleService,
18
+ constructor($L: LocaleService,
1819 private router: Router,
1920 private store: LocalStorageService,
2021 private http: Http) {
21
-
22
+ super($L);
2223 }
2324
2425 public login(username: string, password: string) : Observable<string> {
....@@ -28,7 +29,7 @@
2829 let options = new RequestOptions({ headers: new Headers({ "Content-Type": "application/x-www-form-urlencoded" })});
2930 return this.http.post('user/login', params.toString(), options)
3031 .map((resp) => this.mapLogin(resp))
31
- .catch((err) => this.handleError(err));
32
+ .catch((err) => super.processErrorResponse(err));
3233 }
3334
3435 private mapLogin(res : Response) : string {
....@@ -36,6 +37,7 @@
3637 this.store.set('user_full_name', data.full_name);
3738 this.store.set('username', data.username);
3839 this.store.set('token', data.token);
40
+ console.log('New login token: ' + data.token);
3941 return <string>data.token;
4042 }
4143
....@@ -47,7 +49,7 @@
4749 let option = new RequestOptions({ headers: new Headers({ 'X-SECURIS-TOKEN': token }) });
4850 return this.http.get('check', option)
4951 .map((resp) => this.mapCheck(resp))
50
- .catch((err) => this.handleError(err));
52
+ .catch((err) => super.processErrorResponse(err));
5153 }
5254
5355 private mapCheck(res : Response) : boolean {
....@@ -67,29 +69,6 @@
6769 this.router.navigate(['public/login']);
6870 }
6971
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
- }
9372
9473 }
9574