从方法/发布外部访问Meteor.userId [英] Access Meteor.userId from outside a method/publish

查看:115
本文介绍了从方法/发布外部访问Meteor.userId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为Meteor写一个以服务器为中心的程序包,相关代码如下所示:

I'm currently writing a server-centric package for Meteor, and the relevant code looks something like this:

__meteor_bootstrap__.app.stack.unshift({
    route: route_final,
    handle: function (req,res, next) {
        res.writeHead(200, {'Content-Type': 'text/json'});
        res.end("Print current user here");
        return;
    }.future ()
});

这显然是一种相对棘手的工作方式,但是我需要创建一个RESTful API.

This is obviously a relatively hacky way of doing things, but I need to create a RESTful API.

如何从这里访问Meteor.userId()?文档说只能从方法内部或发布中进行访问.有什么办法解决吗?

How can I access Meteor.userId() from here? The docs say it can only be accessed from inside a method or publish. Is there any way around that?

我尝试过的事情:

  • 使用Meteor.publish("user", function() { user = this.userId() });
  • 从发布中捕获它
  • 从cookie中获取令牌+用户ID,并使用Meteor.users.findOne({_id:userId,"services.resume.loginTokens.token":logintoken});
  • 自己进行身份验证
  • 创建一个名为get_user_id的方法,然后从下面的代码中调用它.
  • Capture it from a publish using Meteor.publish("user", function() { user = this.userId() });
  • Get the token + user id from the cookies and authenticate it myself using something like Meteor.users.findOne({_id:userId,"services.resume.loginTokens.token":logintoken});
  • Create a method called get_user_id and call it from inside my code below.

推荐答案

您首先要定位的目标是获取可以从标头中识别用户的内容(尤其是因为您想要在以下位置获取用户名)没有JavaScript可以运行).

The thing that you need to target first is that to get something that can identify the user from headers (especially because you want to get the username at a point where no javascript can run).

Meteor将登录的会话数据存储在localStorage中,只能通过javascript访问.因此,只有在页面加载完毕并传递了标题之后,它才能检查谁登录了.

Meteor stores session data for logins in localStorage, which can only be accessed via javascript. So it can't check who is logged in until the page has loaded and the headers have been passed.

为此,您还需要将用户数据存储为Cookie以及存储在localStorage上:

To do this you need to also store the user data as a cookie as well as on localStorage:

客户端js -使用w3schools.com的cookie setCookiegetCookie函数

client side js - using cookie setCookie and getCookie functions 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);
    }
});

服务器侧路由

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 ()

cookie允许服务器在呈现页面之前检查谁已登录.用户登录后立即使用Deps.autorun

The cookie allows the server to check who is logged in before the page is rendered. It is set as soon as the user is logged in, reactively using Deps.autorun

这篇关于从方法/发布外部访问Meteor.userId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆