I know there are several threads and I literally tried every solution and I'm not sure where's the difference to my situation.
I want to create a Guard to access the view to change a password after a token was validated by the server.
canActivate(
route: ActivatedRouteSnapshot,
router: RouterStateSnapshot
):
| Observable<boolean | UrlTree>
| Promise<boolean | UrlTree>
| boolean
| UrlTree {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
if (urlParams.has("token")) {
let token = urlParams.get("token");
this.authService.getResetPasswordPermission(token).subscribe(
(res) => {
return true;
},
(err) => {
return false;
}
);
} else {
return this.router.createUrlTree(["/authenticate"]);
}
}
I know that subscribe won't wait for the result and so just return this.router.createUrlTree(["/authenticate"]);
gets executed. I've tried to use map
and pipe
but then it does never go into the result of the method, so neither res
or err
.
And somehow I will always be redirected to localhost:4200 instead of localhost:4200/changePassword where I want to go or even localhost:4200/authenticate where I should land else.
The Observable method that talks to the server looks like this:
getResetPasswordPermission(token): Observable<any> {
let data = { token: token };
return this.http.get<any>(RESET_PASSWORD_PERMISSION_API, { params: data });
}
If it succeeds the answer is a ResponseEntity.ok(token)
.
If not ResponseEntity.badRequest()
. I know they are both not syntactical correct but you get what I mean.
The token is needed again to send it with the change request if I would land on the changePassword page.