Hello, I am new to working with angular2 and ionic2. My goal is to make two requests sequentially after a successful login request.
After a successful login, I want to verify if the returned token has the necessary access rights on the server and redirect the user accordingly.
Below is the code I have tried, but it is not working as expected:
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())
}
}
Code snippet for the _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
});
}