I have the next Angular Guard:
export class AdminGuard implements CanActivate {
constructor(
private readonly router: Router,
private readonly userService: UserService
) { }
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return this.userService.getUserRoles().pipe(
map(roles => {
if (roles.admin) {
return true
}
this.router.navigate(['/'])
return false
})
)
}
}
This is method from the service, which return Observable:
public getUserRoles(): Observable<any> {
return this.store.pipe(
select('user'),
map(({ roles }) => roles)
)
}
And this is works when I navigate between page, but when I directly enter in URL some route, for example 'myapp.com/admin', it returns false automatically and the guard does not work. How to make it work properly?