angular2-authentication-sample
angular2-authentication-sample copied to clipboard
Better way to redirect
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.
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