How to get Meteor.user() to return on the server side? -
in file called /server/main.js (in order ensure loaded last).
console.dir(meteor.user());
throws:
error: meteor.userid can invoked in method calls. use this.userid in publish functions.
so try use, in same file:
console.dir(this.userid);
returns:
undefined
so, not giving up, i'm thinking "that's fine i'll read cookies in header":
var connect = npm.require('connect'); __meteor_bootstrap__.app.use(connect.query()).use(function(req, res, next) { console.dir(req.headers); next(); });
.... returns nothing in terms of cookies except 'cookie: 'uvf=1''
i'm not sure conclude - senseless can otherwise use meteor.account framework fine, read/set user properties, etc. server aware of user, , current user logged in.
i'm @ complete loss, explanation / hint / pointer appreciated.
you have use meteor.user() in place request made client (such meteor.methods or meteor.publish).
it can't placed anywhere else because meteor wouldn't know @ point in code user supposed bound to. if there place request of form made client can this:
in meteor.publish:
meteor.publish("collection", function() { //returns undefined if not logged in check if logged in first if(this.userid) { var user = meteor.users.findone(this.userid); //var user same info given in meteor.user(); } });
in meteor.methods:
meteor.methods({ "test":function() { //should print user details if logged in, undefined otherwise. console.log(meteor.user()); } }
to use meteor.user() on server side route:
you need meteor router installed package via meteorite allow have server rendered page. (installed via mrt install router
)
a server side route handle web request:
meteor.router.add('/awebpage', function(id) { var userid = this.params.userid; var logintoken = this.params.logintoken; var isdirect = this.param.direct; var user = meteor.users.findone({_id:userid,"services.resume.logintokens.token":logintoken}); if(user) { //the user logged in return "you, "+user.profile.name+", logged in!"; } else { if(isdirect) { return "<h3>loading</h3><script>window.location.href="/awebpage?direct=true&userid="+localstorage.getitem("meteor.userid") +"&logintoken="+localstorage.getitem("meteor.logintoken")</script>"; } else { return "not logged in" } } });
so when visit /awebpage
check whether user logged in , thing want when logged in. there redirect relay data localstorage uri.
Comments
Post a Comment