How to implement AuthGuard waiting a connection in Angular 2
Just use map()
instead of subscribe()
. The router does the subscription by itself to initiate the request.
Don't forget to import map
Angular 2 HTTP GET with TypeScript error http.get(...).map is not a function in [null]
I think this should do what you want:
export class AuthGuard implements CanActivate {
constructor(private _api: ApiService) {}
canActivate(): Observable<boolean> {
return this._api.head('/users/self')
.map(response => {
this.doSomethingWithResponse(response.json()));
return true;
})
.catch(err => Observable.of(false));
}
}