**Redirection to state.url within subscription to observable in CanActivate
causes infinite loop**
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
boolean{
/* to store and redirect after login if user not logged in */
let target_url: string = state.url;
let adminSubj = new Subject<boolean>();
return this.verifyLoginForAdmin( target_url );
}
verifyLoginForAdmin( target_url: string ){
if( this.authService.isLoggedin() ){
let token_email =
this.authService
.jwt_helper
.decodeToken( localStorage.getItem(this.authService.jwt_name ) ).email;
this.authService.isAdmin( token_email )
.subscribe({
next: data => {
if( data ){
this.router.navigate( [ target_url ]);
return true;
}
else{
this.router.navigate( [ '/coding-blog' ]);
return false;
}
},
error: error => console.log("CanActivate Error-->", error),
complete: () => console.log("done");
})
}
this.authService.targetUrl = target_url;
this.router.navigate( [ '/coding-blog' ] );
return false;
}
}
**authService#isAdmin() is:**
isAdmin( email: string ): Observable<boolean>{
return this.getDbInfo
.getOneUserByEmail( email )
.map( res => res.json().data.admin );
}
<https://lh3.googleusercontent.com/-CaflsgQiAII/WINMlFjIMVI/AAAAAAAAArI/2Zylrlqidik3RPklSNL3UDFYEBZq-Ev7ACLcB/s1600/consoleEndlessLoopTrue.png>
In another route protected by a different CanActivate implementation
I have the same set up with the `target_url`
and it never goes into a perpetual loop.
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
boolean {
let targetUrl: string = state.url;
return this.verifyLogin( targetUrl, route.params['name'] );
}
verifyLogin(target_url: string, target_user: string): boolean{
if( this.authService.isLoggedin() ){
let token_name =
this.authService
.jwt_helper
.decodeToken( localStorage.getItem( this.authService.jwt_name ) )
.name;
if( token_name === target_user){
this.router.navigate([ target_url ]);
return true;
}
else{
this.router.navigate([ '/projects' ]);
return false;
}
}
this.authService.targetUrl = target_url;
this.router.navigate([ '/projects' ]);
return false;
}
}
**It seems to be the subscription that is doing the
black magic in this sense.**
Obviously I am not understanding this part of this
technology very well.
I think I am lost within Rxjs and Angular2 .. somehow ...somewhere ....
Help please.
--
You received this message because you are subscribed to the Google Groups
"Angular" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.