César Calvo
2017-03-15 199b9ae1c2818713bbb1a33831c46fb505e997d4
securis/src/main/webapp/src/app/user.service.ts
....@@ -12,6 +12,8 @@
1212 @Injectable()
1313 export class UserService {
1414
15
+ count : number = 0;
16
+
1517 constructor(private $L: LocaleService,
1618 private router: Router,
1719 private store: LocalStorageService,
....@@ -25,31 +27,35 @@
2527 params.append('password', password);
2628 let options = new RequestOptions({ headers: new Headers({ "Content-Type": "application/x-www-form-urlencoded" })});
2729 return this.http.post('user/login', params.toString(), options)
28
- .map((res: Response) => {
29
- let data = res.json();
30
- this.store.set('username', username);
31
- this.store.set('token', data.token);
32
- return <string>data.token;
33
- })
34
- .catch((r) => this.handleError(r));
30
+ .map(this.mapLogin)
31
+ .catch(this.handleError);
3532 }
33
+
34
+ private mapLogin(res : Response) {
35
+ let data = res.json();
36
+ //this.store.set('username', data.username);
37
+ //this.store.set('token', data.token);
38
+ return <string>data.token;
39
+ }
3640
3741 isLoggedIn() : Observable<Boolean> {
3842 if (!this.existsToken()) {
3943 return Observable.of(false);
4044 }
4145 var token = this.store.get("token");
42
- return this.http.get('check', new RequestOptions({ headers: new Headers({ 'X-SECURIS-TOKEN': token }) }))
43
- .map((res: Response) => {
44
- let body = res.json();
45
- if (body.valid) {
46
- this.store.set('user', body.user);
47
- }
48
- return body.valid;
49
- })
50
- .catch((r) => this.handleError(r))
51
- .catch(() => Observable.of(false));
46
+ let option = new RequestOptions({ headers: new Headers({ 'X-SECURIS-TOKEN': token }) });
47
+ return this.http.get('check', option)
48
+ .map(this.mapCheck)
49
+ .catch(this.handleError);
5250 }
51
+
52
+ private mapCheck(res : Response) {
53
+ let body = res.json();
54
+ if (body.valid) {
55
+ //this.store.set('user', body.user);
56
+ }
57
+ return body.valid;
58
+ }
5359
5460 existsToken() : Boolean {
5561 return this.store.get("token") !== null;
....@@ -60,6 +66,9 @@
6066 this.router.navigate(['Login']);
6167 }
6268
69
+
70
+
71
+
6372 private handleError (error: Response | any) {
6473 // In a real world app, we might use a remote logging infrastructure
6574 let errMsg: string;