【问题标题】:How to get Meteor.user() to return on the server side?如何让 Meteor.user() 在服务器端返回?
【发布时间】:2013-05-08 02:13:39
【问题描述】:

在一个名为 /server/main.js 的文件中(以确保它最后加载)。

console.dir(Meteor.user());

投掷:

Error: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions.

所以我尝试在同一个文件中使用:

console.dir(this.userId);

返回:

undefined

所以,不要放弃,我在想“没关系,我会从标题中的 cookie 中读取”:

var connect = Npm.require('connect');

__meteor_bootstrap__.app.use(connect.query()).use(function(req, res, next) {
    console.dir(req.headers);
    next();
});

.... 除了 'cookie: 'uvf=1'' 之外,不会返回任何关于 cookie 的内容

我不确定要得出什么结论 - 这是没有意义的,否则我可以很好地使用 Meteor.Account 框架,读取/设置用户属性等。服务器清楚地了解用户,并且清楚地了解当前用户已登录。

我完全不知所措,任何解释/提示/指针将不胜感激。

【问题讨论】:

    标签: meteor


    【解决方案1】:

    您必须在客户端发出请求的地方使用 Meteor.user()(例如 Meteor.methods 或 Meteor.publish)。

    它不能放在其他任何地方,因为流星在那个时候不会知道用户应该绑定到的代码中的那个点。如果有一个地方从客户端发出某种形式的请求,它可以这样做:

    在 Meteor.publish 中:

    Meteor.publish("collection", function() {
        //returns undefined if not logged in so check if logged in first
        if(this.userId) {
            var user = Meteor.users.findOne(this.userId);
            //var user is the same info as would be given in Meteor.user();
        }
    });
    

    在 Meteor.methods 中:

    Meteor.methods({
        "test":function() {
            //should print the user details if logged in, undefined otherwise.
            console.log(Meteor.user());
        }
    }
    

    在服务器端路由上使用 Meteor.user():

    您需要通过meteoriteMeteor router 安装为一个包,以允许您拥有服务器呈现的页面。 (通过mrt install router安装)

    然后服务器端路由可以处理网络请求:

     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 is successfully logged in
    
             return "You, "+user.profile.name+", are 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"
             }
         }
     });
    

    所以现在当您访问/awebpage 时,它会检查用户是否已登录并在他们登录时执行您想要的操作。最初有一个重定向将数据从 localstorage 中继回 URI。

    【讨论】:

    • 谢谢你,Akshat,事情就是这样,我想使用 Meteor.user() 执行条件重定向到不同的 URL。所以必须等待页面加载,初始化所有客户端和服务器端,发布集合,然后从客户端进行方法调用,等待回复,最后“重定向”(此时不会是无论如何,一个 30 倍重定向,因为那艘船已经航行了),好吧,那是行不通的。如果没有办法做到这一点,我将重新评估流星,因为现在我很困惑:(
    • 我想我确实看到了你想要的东西,而且很有可能!。您需要使用服务器端路由器。只要我有一点时间,我就会更新答案,Meteor 很棒,如果你想让它以老式的方式做一些事情,它几乎可以一直不妥协地做到这一点。
    • 非常感谢 Akshat,我很高兴听到您知道如何执行此操作,我已经将您标记为答案 - 期待学习如何执行此操作!跨度>
    • 除非您对它满意,否则我不会接受它。我已经用新内容更新了答案,希望这是您需要它做的事情
    • 谢谢Akshat,我对这个解决方案很满意,尽管它不是服务器端的(即它不是标头重定向)。我还看到它使用迷你页面完成,这是一个类似的解决方案,直到 Meteor 有所发展。再次感谢您!
    【解决方案2】:

    您可以使用 Meteor.publish() 将 userId 公开到全局范围。然后你可以将它与 Meteor.Router 的服务器端路由一起使用。

    --

    /server/publications.js

    CurrentUserId = null;
    Meteor.publish(null, function() {
        CurrentUserId = this.userId;
    });
    

    -

    /server/routes.js

    Meteor.Router.add('/upload', 'POST', function() {
        if (!CurrentUserId)
            return [403, 'Forbidden'];
    
        // proceed with upload...
    });
    

    【讨论】:

    • 它不允许你这样做,因为发布函数应该只返回一个游标或游标数组。
    • 仅当您的应用程序一次只能由一个用户使用时...否则这种方法将惨遭失败,用户将获得查看其他一些随机其他用户的东西的权限,等等。
    • 这太棒了,因为@rdb 状态将完全失败并在多用户环境中施加安全问题,只有发布方法绑定到用户,而其他上下文不存在。
    【解决方案3】:

    你可以使用登录的回调

    Accounts.onLogin((obj)->
      user = ob.user
    )
    
    Accounts.onLogin(function(obj){
      var user = ob.user
    })
    

    【讨论】:

    • 将用户置于全局命名空间可能不是 2015 年的最佳实践。
    • 它的咖啡脚本,所以它不是。用户将是 var'd
    【解决方案4】:

    我最近写了一篇博客文章描述了这个问题的解决方案:https://blog.hagmajer.com/server-side-routing-with-authentication-in-meteor-6625ed832a94

    您基本上需要使用 https://atmospherejs.com/mhagmajer/server-router 包设置服务器路由,并且您可以使用 this.userId 获取当前用户,就像使用 Meteor 方法一样。

    【讨论】:

      猜你喜欢
      • 2020-10-16
      • 1970-01-01
      • 2020-08-27
      • 1970-01-01
      • 1970-01-01
      • 2021-01-28
      • 1970-01-01
      • 1970-01-01
      • 2011-06-07
      相关资源
      最近更新 更多