I've developed a Protection feature that blocks users from directly accessing a specific URL, and it's functioning correctly.
However, the issue arises when I try to refresh the page as the Protection feature ends up redirecting me.
Below is the code snippet:
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 have discovered a method to capture a refresh event using the given code, but it conflicts with this.router.navigated
, rendering it ineffective.
this.subscription = router.events.subscribe((event) => {
if (event instanceof NavigationStart) {
browserRefresh = !router.navigated;
}
});
Is there a way to reconcile these two behaviors in my Protection feature?