我一直在寻找没有运气的解决方案。如果用户被授权,我需要调用服务器,并且我需要 canActivate 方法来等待该调用的结果。但我似乎无法拼凑起来。下面是我的代码,我的问题在代码的注释中。
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
let user: EUser = new EUser();
user.sessionId = localStorage.getItem("sessionId");
//I am not sure how to implement this part.
// I need to recieve the response and get user.isAuthorized value and return it
// as an observable.
this.authenticationService.isAuthorized(user).subscribe((user)=>{
//Here i need to get user.isAuthorized and
//if the value is false, i need to navigate to login with this.router.navigate['login'] and return it.
//if the values it true, i need the code continue as it is.
});
}
身份验证服务
isAuthorized(user:EUser){
return this.http.post(ConnectionHelper.SERVER_URL+
ConnectionHelper.USER+ConnectionHelper.ISAUTHORIZED,user).map(res => res.json());
}