vish
vish

Reputation: 2436

Load page logged in with meteor.js

I am using the built-in Accounts functionality with Meteor.js. When a user is logged in via a cookie, Meteor appears to load the 'logged-out' version of the page first and then automatically log the user in and reload the page to get the the 'logged-in' version.

You can see this behavior in the sample parties app: http://vdawg-parties.meteor.com/ Register, then reload the page. It will load the page logged out and then after 2-3 seconds will switched to the logged in version.

Is it possible to load the logged-in version on page load? It is confusing to load a page and then immediately switch to another page.

Upvotes: 0

Views: 349

Answers (1)

Tarang
Tarang

Reputation: 75945

Meteor is built off a "data on the wire" type structure. This means that initially, when your project loads no actual data has been sent between the server and browser. Because of this there is no way to determine whether a user is logged in or not in a meteorish way without waiting for the data to be downloaded.

This is the reason meteor stores login data in localStorage when a user is logged in.

Loading screen

It's best to have a loading screen while meteor is downloading/syncing the local collections from the server, in a way that you wait for the user's login to be completed:

client side html

<template name="YourLoggedInSection">
    {{#if isLoaded}}
        ..your logged in section/or place to log in
    {{else}}
        <h1>Loading</h1>
    {{/if}}
</template>

Client side js

Template.YourLoggedInSection.isLoaded = function() {
    return Accounts.loginServicesConfigured();
}

Upvotes: 1

Related Questions