I have an Auth pipeline.
run(navigationInstruction, next) {
if (navigationInstruction.getAllInstructions().some(i => i.config.auth)) {
var isLoggedIn = this.auth.isAuthenticated()
if (!isLoggedIn) {
return next.cancel(new Redirect('login'));
}
}
return next();
}
As you can see it checks if an unauthenticated user tries to access an auth page they get redirected to the login page. This works. Now I'm trying to achieve the reverse of that if the user is logged in and tried to access a non authentication page I want to redirect them to the dashboard
run(navigationInstruction, next) {
var isLoggedIn = this.auth.isAuthenticated()
if (navigationInstruction.getAllInstructions().some(i => i.config.auth)) {
if (!isLoggedIn) {
return next.cancel(new Redirect('login'));
}
} else {
if (isLoggedIn) {
return next.cancel(new Redirect('dashboard'));
}
}
return next();
}
Now this also works however it causes a bug I believe. If I try to access a non authorization page and I am not authorized it redirects to the login page. So a more specific example is I'm on the login page and I am not authenticated and I'm trying to go to the register page I just get redirected to the login page. Now I thought that for some reason i.config.auth was thinking it was true but I did some debugging and I realized the return next() actually gets hit it loads the register template but then it redirects back to the login page.
I've also noticed that just moving is var isLoggedIn function outside of the if statement the same bug occurs:
run(navigationInstruction, next) {
var isLoggedIn = this.auth.isAuthenticated()
if (navigationInstruction.getAllInstructions().some(i => i.config.auth)) {
if (!isLoggedIn) {
return next.cancel(new Redirect('login'));
}
}
return next();
}
UPDATE
Here are my routes
config.map([
{ route: '', redirect: 'login' },
{ route: 'login', moduleId: 'components/login/login', title: 'Login', name: 'login' },
{ route: 'register', moduleId: 'components/register/register', title: 'Register', name: 'register' },
{ route: 'dashboard', moduleId: 'components/dashboard/dashboard', title: 'Dashboard', auth: true, name: 'dashboard' },
]);
isLoggedInis always what I would expect even inside the if statements - Rodrigoauthproperty is indeed false or undefined on the 'register' route. So if I understand your question correctly: you are not logged in and you navigate from 'login' to 'register'. First,rungets hit andreturn next();is called. Second,rungets hit again andreturn next.cancel(new Redirect('login'));is called. Is this correct? - Fred Kleuverreturn next()gets hit but for some reason when going from login page to register page it redirects back to the login page. Maybe it doesn't redirect but I can see on my console that the register template was loaded but the page just flickers and stays on the login page - Rodrigo