How to ensure that Accounts.onLogin() is finished before Templates are rendered?

Having an issue with how my Electron app is starting up. Very early into Accounts.onLogin() I’m calling the backend to get the stored values for the most important session variables in my app.

However, before this call is returned and the values are being assigned, the first element of my top navigation bar Template is already triggered.

As this navigation bar is dependent on one of the session variables, this is causing a race condition.

Is there a way how I can ensure that my Accounts.onLogin function is fully executed before the app starts to render Templates? I know this will lead to longer “time to paint” for the user but the current problems caused by the race conditions are worse.

Thanks in advance!

Make elements in the navigation bar dependent on your var and not the whole bar or top level components, views, templates.
You can leave placeholders (invisible if needed) to not affect the navigation bar layout when components are missing.

You can use this in either your template initialization code somehow and/or your template:

Accounts | Meteor API Docs (Template Helper)

Which should return false once the login process is complete. We use it in autoruns like this:

`if (Meteor.user() && !Meteor.loggingIn())``

(careful: Meteor.user() creates a reactive dependency on all fields of the user object. You can also only use Meteor.userId() for example if you don’t really need the whole object.)

Thanks Daniel for your code reference.

Would this be the correct way (return false when the login isn’t finished)?

Template.navbarLogged.onCreated(function () {
    if (Meteor.userId() && !Meteor.loggingIn()) {
        console.warn(`navbar-logged.js: 161 - Template.navbarLogged.onCreated - login is finished`);
    } else {
        console.warn(`navbar-logged.js: 163 - Template.navbarLogged.onCreated - login is not finished`);
        return;
    }

I could also use a Session variable that is true once the code in the Accounts.onLogin() function has advanced far enough and then check that Session variable at the start of the Template.onCreated() - correct?

Regards,

Andreas

Code looks nice, but you should wrap it in an autorun block to make sure the code reruns once the dependencies change.

And yes, you could then store / update the “ready” state of eg. a reactive variable.

I’m a big fan of both this.autorun (where this is the template instance) so the autorun gets stopped automatically once your done, and also this guy: GitHub - meteor-space/template-controller: Syntactic sugar for best practice Blaze templates to make writing code for templates more easy & tight.

Have a look!

You’re not on blaze I think? Because otherwise, couldn’t you just use the {{ #unlessloggingIn }} template helper around your template?

ALSO:

Ah, hot tipp: To avoid the mess with the reactive variable, you could just stuff this in a template HELPER.

That’s reactive / wrapped in an autorun automatically & also it returns its current value. You could just stuff your if condition in a helper and call it a day.

1 Like