0
export class RouteNavigateService implements CanActive {
   private loggedIn: boolean = false;
   private navigateTo: boolean = false;

   constructor(private router: Router){ }

   canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
      // check user is logged
      ...

      // check that we request the page from code and not changing the URL
      const nav = this.router.getCurrentNavigation();
      if(nav?.id !== 1) {
         this.navigateTo = true;
      } else {
         this.navigateTo = false;
      }

      return this.loggedIn && this.navigateTo;
}

As you can see from the picture, I have a Route Guard that has to admit the navigation to a specific page only if the user is logged (first part) and only if the page is called from code and not modifing the URL path.

My problem is that my project has also to manage the reload of a page and this event go in conflict with the second part of the check because in both cases the nav.id is iequal to 1. I tried to check also other parameter of the available arguments (nav, state and route) but nothing give me the possibility to manage this conflict.

How can I solve this problem? Has someone encounter a similar problem?

Edited

I added the code instead of using the image.

What I need to understand is how to identify the situation of reload of the page, considering that, also in this case, the nav.id is equal to 1 like the case of navigate via URL change. This because I need to managed the reload and not simply block it, while I need to block the navigation via URL.

Fabio C.
  • 1
  • 1

0 Answers0