[SOLVED] Why does Accounts.onLogin get called on every page request after signing in?

According to the docs, Accounts.onLogin allows for a registering a callback after a login attempt succeeds.

However, in reality, Accounts.onLogin gets called not just after a user signs in, but for every page request thereafter until they sign out. Why?

This makes it hard to use this method for something specific to just a login event (such as redirecting back to the originally requested page.)

Why this is happening? How I can can prevent it from happening? I want to only call this once after a user signs in.

To see an example, and the code that triggers this issue, please see the following repo: https://github.com/andersr/meteor-accounts-login-test

  • While anonymous, the function is not called.
  • After signing in, it gets called with every new page request.

Here’s a workaround:

  1. When redirecting to the login route, put the original path in a ?next= query property.
  2. Have youronLogin code will look for the next and redirect to it if it’s there.
1 Like

This seems like a great solution Would you be able to point me to a code sample? Specifically, I’m not clear on how to put the original path into a query property.

1 Like

Check out what returns from FlowRouter.current(), the query parameters are probably accessible there. Then, you can pass it to FlowRouter.go() or FlowRouter.path()

I do not see this behavior in our production Meteor app. For us, Accounts.onLogin is only called once. We use Blaze + FlowRouter.

Perhaps this is an issue with React + FlowRouter + Accounts.onLogin? I’d try isolating it down to the simplest app that reproduces the behavior. Ideally you could reuse the same app logic and show the behavior in two identical apps but one running Blaze and one running React.

1 Like

@alanning I followed your advice and you are correct it does not occur every time. Now all I have to figure out is what is causing Accounts.onLogin to be called in my production app but at least I know the issue is on my end. Thx!

Cool. Glad you got it isolated. I saw the repro you linked before but it wasn’t minimal (since it had some other things going on like the redirect) so I figured a simpler one may help debug.

One thing that may help is making Accounts.onLogin throw after the 2nd call or so. That way you can walk back the stack trace and maybe see what’s triggering it.

Just a guess but it may be tied to private browsing and/or localStorage and cookies not being writable for storing the login token.

Ok, thanks for the suggestions. Yes, I did repurpose an existing repo for that test so it wasn’t as minimal as it could have been.

Hi, did you get any result? I’m running into the same problem, it would be helpful if you could share your result on the issue, thanks

seems i have the same issue

Accounts.onLogin(() => {
console.log(‘onlogin() called’);
BlazeLayout.render(‘layoutHTMLIndex’, {main: “klemmbrett”, navbar: “standard”});
});

FlowRouter.route(’/teams’, {
action() {
if (Meteor.userId() != null){
BlazeLayout.render(‘layoutHTMLIndex’, {main: “teams”, navbar: “teamsbutton”});
}
else{
BlazeLayout.render(‘layoutHTMLIndex’, {main: “welcome”, navbar: “standard”});
}
}
});

when i direct to http://localhost:3000/teams onLogin() is called again.

console…
Navigated to http://localhost:3000/teams
routes.js:6 onlogin() called

what was the solution to the issue above?