I've created a Guard that prevent a user to manually access an URL and it's working fine.
The problem is that when I refresh the page, the Guard redirects me.
Here's the code:
export class NavigationGuard implements CanActivate {
constructor(private router: Router) { }
canActivate(): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
if (this.router.navigated) {
return true
} else {
this.router.navigate(['/'])
return false
}
}
}
I've seen that I can use the following code to get a refresh event but it's basically the opposite of this.router.navigated
so it's not working.
this.subscription = router.events.subscribe((event) => {
if (event instanceof NavigationStart) {
browserRefresh = !router.navigated;
}
});
Is it possible that have those 2 behaviours working at the same time in my Guard ?