0

I'm trying to create an guard to verify the token via HTTP call as CanActivate accepts return types as Observable<boolean>.

Though the HTTP request is hitting the backend (logs and db calls) still in front end it shows as cancelled.

Below is my code:

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
    if (this.isTokenPresent(route)) {
        return this.validateToken(route).pipe(map((e: any) => {
                console.log(e);
                return true;
            }),
            catchError((err) => {
                return of(false);
            }));
    } else {
        return false;
    }
}

private validateToken(route: ActivatedRouteSnapshot) {
    return this.httpClient.get(this.Url + "/v1/tax/verify?token=" + route.queryParams.token, {responseType: 'text' as 'json'})
}
Gaël J
  • 11,274
  • 4
  • 17
  • 32
Azeem
  • 46
  • 5
  • How do you know if it is complete from backend? Do you see a http 200 status in network tab? – Ritesh Waghela Jun 17 '21 at 10:54
  • no in the network tab it shows as cancelled, – Azeem Jun 17 '21 at 11:26
  • 1
    Does https://stackoverflow.com/questions/12009423/what-does-status-canceled-for-a-resource-mean-in-chrome-developer-tools answer your question? – deepakchethan Jun 17 '21 at 11:27
  • Do you have more messages in the console like why it was cancelled? Maybe CORS issue for instance.. – Gaël J Jun 17 '21 at 11:28
  • @GaëlJ nothing is printed on the cancelled, on the network tab status is cancelled – Azeem Jun 17 '21 at 11:37
  • @Azeem That means your call is not succeeding. Somehow it is getting unsubscribed. Do you have other guards too for that route, post the code where you are referencing that guard? – Ritesh Waghela Jun 17 '21 at 11:39
  • Iits a new project I only have one ... FYI angular-routing.ts { path: 'tax-decleration', component: TaxDecelerationComponent, canActivate : [InviteTokenGuard] }, – Azeem Jun 17 '21 at 11:45
  • i'm getting data when i'm manually subscribing to it, but its not when i'm returning it as Observable as needed by CanActivate. Is there any alternate way i can acheive this(async call in router gurad) – Azeem Jun 17 '21 at 19:17
  • Can't be sure since I can't see it in your code, but can you comment out if (this.isTokenPresent(route)) part and see what happens? Wondering if that is the part breaking things. – usalin Jun 17 '21 at 21:31
  • It just check whether the route contains token query param or not. – Azeem Jun 17 '21 at 22:44

0 Answers0