rsanchez
2017-03-10 2762277c60db7df5ad3214b10a0dd93d4f2f1128
securis/src/main/webapp/src/app/user.service.ts
....@@ -1,3 +1,4 @@
1
+import { LocaleService } from './common/i18n';
12 import { Injectable } from '@angular/core';
23 import { Router } from '@angular/router';
34 import { Location } from '@angular/common';
....@@ -11,7 +12,8 @@
1112 @Injectable()
1213 export class UserService {
1314
14
- constructor(private router: Router,
15
+ constructor(private $L: LocaleService,
16
+ private router: Router,
1517 private store: LocalStorageService,
1618 private http: Http) {
1719
....@@ -22,14 +24,14 @@
2224 params.append('username', username);
2325 params.append('password', password);
2426 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)
2628 .map((res: Response) => {
2729 let data = res.json();
2830 this.store.set('username', username);
2931 this.store.set('token', data.token);
3032 return <string>data.token;
3133 })
32
- .catch(this.handleError);
34
+ .catch((r) => this.handleError(r));
3335 }
3436
3537 isLoggedIn() : Observable<Boolean> {
....@@ -45,7 +47,7 @@
4547 }
4648 return body.valid;
4749 })
48
- .catch(this.handleError)
50
+ .catch((r) => this.handleError(r))
4951 .catch(() => Observable.of(false));
5052 }
5153
....@@ -62,12 +64,21 @@
6264 // In a real world app, we might use a remote logging infrastructure
6365 let errMsg: string;
6466 if (error instanceof Response) {
65
- const body = error.json() || '';
66
- const err = body.error || JSON.stringify(body);
67
+ const err = JSON.stringify(error);
6768 errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
6869 } else {
6970 errMsg = error.message ? error.message : error.toString();
7071 }
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
+
7182 console.error(errMsg);
7283 return Observable.throw(errMsg);
7384 }