| .. | .. |
|---|
| 12 | 12 | @Injectable() |
|---|
| 13 | 13 | export class UserService { |
|---|
| 14 | 14 | |
|---|
| 15 | + count : number = 0; |
|---|
| 16 | + |
|---|
| 15 | 17 | constructor(private $L: LocaleService, |
|---|
| 16 | 18 | private router: Router, |
|---|
| 17 | 19 | private store: LocalStorageService, |
|---|
| .. | .. |
|---|
| 25 | 27 | params.append('password', password); |
|---|
| 26 | 28 | let options = new RequestOptions({ headers: new Headers({ "Content-Type": "application/x-www-form-urlencoded" })}); |
|---|
| 27 | 29 | 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); |
|---|
| 35 | 32 | } |
|---|
| 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 | + } |
|---|
| 36 | 40 | |
|---|
| 37 | 41 | isLoggedIn() : Observable<Boolean> { |
|---|
| 38 | 42 | if (!this.existsToken()) { |
|---|
| 39 | 43 | return Observable.of(false); |
|---|
| 40 | 44 | } |
|---|
| 41 | 45 | 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); |
|---|
| 52 | 50 | } |
|---|
| 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 | + } |
|---|
| 53 | 59 | |
|---|
| 54 | 60 | existsToken() : Boolean { |
|---|
| 55 | 61 | return this.store.get("token") !== null; |
|---|
| .. | .. |
|---|
| 60 | 66 | this.router.navigate(['Login']); |
|---|
| 61 | 67 | } |
|---|
| 62 | 68 | |
|---|
| 69 | + |
|---|
| 70 | + |
|---|
| 71 | + |
|---|
| 63 | 72 | private handleError (error: Response | any) { |
|---|
| 64 | 73 | // In a real world app, we might use a remote logging infrastructure |
|---|
| 65 | 74 | let errMsg: string; |
|---|