angular2-authentication-sample icon indicating copy to clipboard operation
angular2-authentication-sample copied to clipboard

Better way to redirect

Open mrpotato3 opened this issue 9 years ago • 1 comments

Hi!

after many test I think this is the best way to redirect in LoggedInOutlet.ts.

activate(ci:ComponentInstruction)
{
  let url = this.parentRouter.lastNavigationAttempt;
  let route;

  if (!this.publicRoutes[url] && !localStorage.getItem('jwt'))  // !isAuth + !publicRoute -> Redirect to login
    route = ['Login'];
  else if (this.publicRoutes[url] && localStorage.getItem('jwt'))  // isAuth + publicRoute -> Redirect to dashboard
    route = ['Main'];
  else  // Continue routing without changes
  {
    this.parentRouter.navigateByUrl(url);
    return super.activate(ci);
  }

  this.parentRouter.navigate(route);
  return super.activate(this.parentRouter.generate(route).component);
}

This avoids to show other parts of the web.

I hope this helps.

mrpotato3 avatar Feb 12 '16 19:02 mrpotato3

This is a great suggestion, but at my attempt the url have now an additional "/" in front of them so I had to add that to the publicRoutes. But this fixed a Bug that I had on my instance.

Thanks @mrpotato3

dmastag avatar Jun 23 '16 15:06 dmastag