Meteor.loginWithPassword redirects

Today I updated iron:router from 0.9.4 to the newest version. I can’t imagine this is caused by iron router but since then, when I call Meteor.loginWithPassword, even if it’s just Meteor.loginWithPassword('', '') from the console, I get redirected from /login to /. I have no idea why this could happen. I don’t get any logs in the console, just server-side logs telling me the login has failed.

Any idea?

1 Like

Yeah, it is something in your code. When I setup a virgin project with iron router and accounts password this does not happen

I am actually having a similar issue. Meteor.loginWithPassword() some how triggers a route to ‘/’. I can see this happening because I have a Iron Router global Router.onBeforeAction being triggered regardless of whether the login succeeds of fails. @fvg did you ever figure this out?

It happens with me too, probably because of the reactivity of the hooks.

@fvg @borges Hey guys. I actually ended up figuring it out for my app. Hopefully my issue will help solve yours. So in my Iron Router code, I have a typical global Router.onBeforeAction hook that checked on all route changes if a user was logged in. My issue was that I was also checking while a user was loggingIn as well. The below change fixed the redirect that was happening.

This caused the unwanted reload

Router.onBeforeAction(function() {
if (Meteor.userId()) {
this.next();
} else if(Meteor.loggingIn()) {
//Insert logic
}else{
Router.go(’/’);
}
});

This fixed it so there was no reload

Router.onBeforeAction(function() {
if (Meteor.userId()) {
this.next();
}else{
this.redirect(’/’);
}
});

1 Like

Impossible to be simpler than that. Thanks!

@borges Glad I could help! It drove me crazy last night :stuck_out_tongue_closed_eyes: