javascript - Meteor: How to detect if users authenticated -
in app use accounts-github
. works perfect, have 1 problem.
in 1 of templates do
template.bar.rendered = function () { if (meteor.user()) { // setup stuff } }
the problem if user not logged in code not executed (thats ok). when user authenticates code not executed again. question how can listen change inside template (doesn't have in inside rendered
function!)?
you use deps.autorun
. (http://docs.meteor.com/#deps_autorun)
usually deps.autorun
run whole meteor app. if want make runs per template need create , stop in rendered , destroyed template callbacks
e.g
var loginrun; template.bar.rendered = function() { loginrun = deps.autorun(function() { if(meteor.user()) { //stuff run when logged in } }); } template.bar.destroyed = function() { loginrun.stop(); }
if don't need run per template (need run once app on template, can use deps.autorun
on own, anywhere in client side code.
meteor.user()
reactive, ensure deps.autorun callback runs again when changes, theoretically use things when user logs in or out.
other alternatives there package on atmosphere provides login , logout hooks, though use deps.autorun above work anyway. see https://github.com/benjaminrh/meteor-event-hooks
Comments
Post a Comment