私は運がない解決策を探していました。ユーザーが承認されていればサーバーを呼び出す必要があり、その呼び出しの結果を待つメソッドをcanActivateする必要があります。しかし、私は一緒に作品を置くことはできません。以下は私のコードです、私の質問はコードのコメントです。canActivateメソッドからObservable <boolean>を返し、falseにリダイレクト
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.
});
}
AuthenticationService
isAuthorized(user:EUser){
return this.http.post(ConnectionHelper.SERVER_URL+
ConnectionHelper.USER+ConnectionHelper.ISAUTHORIZED,user).map(res => res.json());
}
バックエンドの応答はどのように見えますか? –
username、sessionId、isAuthorizedフィールドを持つjsonオブジェクト。 @ Jota.Toledo – rematnarab