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

查看:16
本文介绍了从方法/发布外部访问 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天全站免登陆