1

I have a guard protecting some routes.

In case when user has no access to the specified route the guard should do the following:

  • Do nothing if navigation was made inside Angular application (routerLink)
  • Navigate to home page if it's standard browser navigation (it causes Angular initialization)

What should I check to know which execution path to choose?

Andrii Romanchak
  • 740
  • 1
  • 12
  • 27

1 Answers1

0

Take a look at this link https://v2.angular.io/docs/ts/latest/guide/router.html#!#query-parameters . Inside your authGuard implementation you can get current url and check usingRouterStateSnapshot and ActivatedRouteSnapshot from '@angular/router' module. To know if user is already inside Angular you can use this APP_INITIALIZER. This is a function that will be executed when application is initialized. Here is an implementation How to call an rest api while bootstrapping angular 2 app. The idea is to use sessionStorage to store if user is coming from outside of Angular or not. Then within your AuthGuard you can check that value from sessionStorage (which is destroyed once page is destroyed. To make sure you can still use.

Ulrich Dohou
  • 1,509
  • 14
  • 25