Canactivate is returning false always coming from API condition check angular

  angular, canactivate

I have used canActivate to protect a page but it always returning false because of that I can not access the protected router. I tried many ways, but was not successful to fix the issue. I am new to angular and i am stuck in validating the condition if (data.hasPermission == true).

can any one please help me.

auth.service.ts

//Checking user access
    getUserAccess(name: string): Observable<any> {

        return this.http.get(`api/${name}/useraccess`).pipe(
            map(
                (response: any) => {
                   
                    return response;
                },
            ),
        );
    };


checkUserPermission() {
 

        this.getUserAccess(this.name).subscribe(data => {
this.hasaccess = false;
         
           if (data.hasPermission == true) {
                this.hasaccess = true;
            } else {
                 this.hasaccess = false;
           }
           
        });
         return this.hasaccess
    }


isUserHasAccess(): boolean {
 return this.hasaccess
}

auth.guard.ts

canActivate(route: ActivatedRouteSnapshot,
        state: RouterStateSnapshot): boolean {
if (this.authService.isUserHasAccess()) {

            return true;
        } else {
            return false;
        }
}

json data from API

{"hasPermission":true}

Source: Angular Questions

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.