What you need to migrate is to get what can identify the user from the headers (especially because you want to get the username at the point where javascript cannot work).
Meteor stores session data for entering localStorage , which can only be accessed through javascript. Therefore, he cannot verify who signed up until the page was loaded and the headers were transferred.
To do this, you also need to save the user data as a cookie, as well as on localStorage :
client side js - using setCookie and getCookie from w3schools.com
Deps.autorun(function() { if(Accounts.loginServicesConfigured() && Meteor.userId()) { setCookie("meteor_userid",Meteor.userId(),30); setCookie("meteor_logintoken",localStorage.getItem("Meteor.loginToken"),30); } });
server route
handle: function (req,res, next) { //Parse cookies using get_cookies function from : http://stackoverflow.com/questions/3393854/get-and-set-a-single-cookie-with-node-js-http-server var userId = get_cookies(req)['meteor_usserid']; var loginToken = get_cookies(req)['meteor_logintoken']; var user = Meteor.users.findOne({_id:userId, "services.resume.loginTokens.token":loginToken}); var loggedInUser = (user)?user.username : "Not logged in"; res.writeHead(200, {'Content-Type': 'text/json'}); res.end("Print current user here - " + loggedInUser) return; }.future ()
A cookie allows the server to check who is logged in before the page is displayed. It is installed as soon as the user is logged in using Deps.autorun
source share