angular2 및 ionic2를 처음 사용하고 다른 성공적인 요청 후에 두 개의 요청을 수행하고 싶습니다.
그것은 성공적인 로그인 후 반환 된 토큰에 서버에 대한 액세스 권한이 있는지 확인하고 그에 따라 사용자를 다시 수정하고 싶습니다.
이것은 내가 시도했지만 작동하지 않습니다.
redirrect(token: string) {
//console.log(token) returns value of the token
if (this._authservice.checkAccessRights(token, "is-marshal")) {
this._navCtrl.setRoot(MarshalPage);
} else if(this._authservice.checkAccessRights(token, "can-read")) {
this._navCtrl.setRoot(UserPage);
} else {
//force logout then redirrect to login page
return this._authservice.logout()
.subscribe(() => {
this.showtoast("No access rights");
this._navCtrl.setRoot(LoginPage);
},
error=>this.handleError())
}
}
이것은 _authservice입니다
checkAccessRights(usertoken: string, permission: string): Observable<any>
{
let headers = new Headers();
headers.append('Authorization', 'Bearer ' + usertoken);
return this._http.post(this.authurl +"can-access",permission)
.map((response: Response) => {
return response; //this response is true or false from server
});
}
checkAccessRights
관찰 가능 항목을 반환하므로 구독하고 참 / 거짓을 확인해야합니다. 현재는 Observable이 반환되는지 확인하고 있습니다.
시험:
redirrect(token: string) {
//console.log(token) returns value of the token
this._authservice.checkAccessRights(token, "is-marshal").subscribe(
data=>{
data?
this._navCtrl.setRoot(MarshalPage):
this._authservice.checkAccessRights(token, "can-read").
subscribe(data=>{
if(data)
this._navCtrl.setRoot(UserPage);
else
this._authservice.logout()
.subscribe(() => {
this.showtoast("No access rights");
this._navCtrl.setRoot(LoginPage);
},
error=>this.handleError());
//force logout then redirrect to login page
});
});
}
switchmap 사용 :
redirrect(token: string) {
canRead$ = this._authservice.checkAccessRights(token, "can-read").switchmap(data=>data?Observable.fromPromise(this._navCtrl.setRoot(UserPage)).mapTo(true)):
this._authservice.logout().mapTo(false));
marshal$ = this._authservice.checkAccessRights(token, "is-marshal").switchmap(data=>{data? Observable.fromPromise(this._navCtrl.setRoot(MarshalPage)).mapTo(true):
canRead$);
marshal$.subscribe(data=>{
if(!data){
this.showtoast("No access rights");
this._navCtrl.setRoot(LoginPage);
},
error=>this.handleError());
//force logout then redirrect to login page
});
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다