Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meteor.loggingIn() waits for all subscriptions to finish before changing its state #10432

Open
ni-ko-o-kin opened this issue Jan 24, 2019 · 5 comments
Labels
confirmed We want to fix or implement it Project:Accounts:Password

Comments

@ni-ko-o-kin
Copy link

ni-ko-o-kin commented Jan 24, 2019

Meteor: 1.8.2
OS: Ubuntu 19.04
Browser: Chrome 76, Firefox 71
reproduction repo: https://github.com/ni-ko-o-kin/meteor-loggingin-subscriptions

The expected behavior:
When a user logs in (with username: 'username' and password: 'password' in the reproduction-repo) Meteor.loggingIn() should return true and after it is finished the subscriptions should start to load.

In the reproduction-repo, after logging in, the page should first show 'logging in', then 'subs loading' and then the numbers of links.

The actual behavior:
After the user logs in, Meteor.loggingIn() return true until all subscriptions are loaded.

In the reproduction-repo, after logging in, the page shows 'logging in' until all subscriptions are loaded. 'subs loading' is never displayed.

This only happens when the user logs in, but not when the user is already logged in and refreshes the page!

You can login with Meteor.loginWithPassword('username', 'password').

@ni-ko-o-kin ni-ko-o-kin changed the title Meteor.logginIn() waits for all subscriptions to fnished before changing its state Meteor.logginIn() waits for all subscriptions to finish before changing its state Feb 6, 2019
@stale
Copy link

stale bot commented Dec 10, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added stale-bot and removed stale-bot labels Dec 10, 2019
@ni-ko-o-kin
Copy link
Author

Bug is still there with Meteor 1.8.2.

@filipenevola filipenevola added the confirmed We want to fix or implement it label Dec 11, 2019
@filipenevola
Copy link
Collaborator

thanks @ni-ko-o-kin , I just labeled as confirmed so stale bot is going to ignore it.

@stale
Copy link

stale bot commented Oct 31, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale-bot label Oct 31, 2020
@ni-ko-o-kin ni-ko-o-kin changed the title Meteor.logginIn() waits for all subscriptions to finish before changing its state Meteor.loggingIn() waits for all subscriptions to finish before changing its state Nov 2, 2020
@WilliamKelley
Copy link
Contributor

I do not think this is a bug. This is canonical behavior that occurs because the publication depends on this.userId which is a reactive data source (setup internally with Tracker).

When you use Meteor.loginWith*** after the page loads, the client has already subscribed to the publication, which has been initialized for that connection and the dependency tracked. During a successful login, setUserId is called. This can cause the publication to immediately react (i.e. Fibers.yield), interrupting the login from returning, and start publishing those docs. Once the publication is done and returns, the server-side login finishes, returns to the client, and the client finishes, setting loggingIn to false.

This is in contrast to when you reload the page; the accounts-base package runs _initLocalStorage on load (before your component renders) and calls loginWithToken. In this case, the subscription has not been initialized, and the dependency is not tracked. Hence, there's no publication associated with the connection to interrupt the login process when the user id is set. Instead the login success returns to the client immediately. Only once this is done does your component get a chance to render and start the subscription.

The code was written in awareness of these facts:

  • accounts-client noted that all subscriptions will be rerun and ready by the time loggingIn will be set to false, here
  • ddp-common noted subscriptions will be rerun in the definition of setUserId on the DDP MethodInvocation class, here

As for a workaround, if you want to avoid this behavior for a given publication, you should condition your subscribe calls to only occur when the user is logged-in.

let handle
if (Meteor.userId()) {
  handle = Meteor.subscribe('links.list');
} else {
  handle = undefined;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed We want to fix or implement it Project:Accounts:Password
Projects
None yet
Development

No branches or pull requests

4 participants