0

I have application which shows access denied when user tries to access unauthorized page (using canactive) however if user requests access denied page itself in the url I should be avoiding it's navigation to access denied page, instead user must be navigated to home page.

Kindly let me know the possibilities,

Akshata7
  • 19
  • 4
  • 1
    `CanActivate` can return a boolean indicating whether or not the user has access to the route, but it can also return a [`UrlTree`](https://angular.io/api/router/UrlTree) that specifies where the router should take the user if they are not authorized. – D M Jun 09 '21 at 03:10
  • Does this answer your question? [Redirect to a different component inside @CanActivate in Angular2](https://stackoverflow.com/questions/34711889/redirect-to-a-different-component-inside-canactivate-in-angular2) – D M Jun 09 '21 at 03:12
  • If you want to detect whether the user manually entered the route as a URL (instead of clicking on a `routerLink` in the application), there is some information about getting the previously active route in [this answer](https://stackoverflow.com/a/63130858/14956277). – D M Jun 09 '21 at 03:17
  • I don't have canactive for access denied page, it is there for other modules only. – Akshata7 Jun 09 '21 at 03:24
  • Ahh, I misunderstood your question. You will likely want to implement `CanActivate` for your access denied page and add a flag somewhere (service, `AppComponent`, [`sessionStorage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage), etc) that indicates the routing action is being done because the user was not authorized, not because the user manually entered the address. The guard on your access denied page would check this flag to see if the user should be shown the access denied page or if they should be redirected to the home page. – D M Jun 09 '21 at 04:11
  • Ok I will try in that way. Thank you – Akshata7 Jun 09 '21 at 10:49

0 Answers0