【问题标题】:Node Fibers Trouble with MeteorMeteor 的节点光纤故障
【发布时间】:2013-06-16 14:01:23
【问题描述】:

我正在尝试为 Meteor 编写一个简单的身份验证后端,该后端针对 LDAP 服务器进行身份验证。我需要注册为登录处理程序的函数(Accounts.registerLoginHandler 的输入)来返回刚刚登录的用户的id

我认为问题在于我创建的 Fiber getUserId,它没有像我想要的那样返回 id。我知道它必须在光纤中,否则流星会​​生气并引发错误。即使yield 之前的日志显示id 不是未定义的,getUserId.run() 始终返回未定义。

任何帮助将不胜感激,谢谢!

Accounts.registerLoginHandler(function(loginRequest) { 

    console.log("In login handler");

    return auth.authenticate(loginRequest.username, loginRequest.password, function(err, ldap_user) {

        if (err){
            // ldap authentications was failed
            console.log("Login failed");
            return undefined;
        }
        else {
            // authentication was successful
            console.log("Login success");

            // extracting team name from ldap record
            var equals = ldap_user.memberOf.indexOf("=");
            var comma = ldap_user.memberOf.indexOf(",");
            var team_name = ldap_user.memberOf.slice(equals+1,comma);

            // add user if they don't already exist

            var getUserId = Fiber( function() { // Meteor code must be ran within a fiber
                var id = null;
                var user = Meteor.users.findOne({username: loginRequest.username});
                if(!user) {

                    // insert user and kick back id
                    id = Meteor.users.insert({username: loginRequest.username,
                                              profile : {team : team_name}
                                             });
                    console.log('no user found, creating' + id);

                } else { 

                    id = user._id; 
                    console.log('user found, returning id' + id);

                }

                console.log('id: '+id);
                Fiber.yield(id);  // return id
            });

            // send logged in users if by executing the fiber
            return {id: getUserId.run()};
        }
    });
});

【问题讨论】:

    标签: javascript node.js meteor future node-fibers


    【解决方案1】:

    我认为问题与需要使用Meteor.bindEnvironment 来控制(环境)变量和正在使用的纤维的范围有关。

    这里有一个很好的关于这个主题的三步教程:

    1. https://www.eventedmind.com/feed/nodejs-introducing-fibers
    2. https://www.eventedmind.com/feed/meteor-dynamic-scoping-with-environment-variables
    3. https://www.eventedmind.com/feed/meteor-what-is-meteor-bindenvironment

    我对您的代码的看法是这样的(在类似的问题中对我有用):

    Accounts.registerLoginHandler(function(loginRequest) { 
    
        console.log("In login handler");
    
        var boundAuthenticateFunction = Meteor.bindEnvironment(function(err, ldap_user) {
            if (err){
                // ldap authentications was failed
                console.log("Login failed");
                return undefined;
            }
            else {
                // authentication was successful
                console.log("Login success");
    
                // extracting team name from ldap record
                var equals = ldap_user.memberOf.indexOf("=");
                var comma = ldap_user.memberOf.indexOf(",");
                var team_name = ldap_user.memberOf.slice(equals+1,comma);
    
                // add user if they don't already exist
    
                var id = null;
                var user = Meteor.users.findOne({username: loginRequest.username});
                if(!user) {
    
                    // insert user and kick back id
                    id = Meteor.users.insert({username: loginRequest.username,
                                              profile : {team : team_name}
                                             });
                    console.log('no user found, creating' + id);
    
                } else { 
    
                    id = user._id; 
                    console.log('user found, returning id' + id);
    
                }
    
                console.log('id: '+id);
    
                return {id: id};
            }
        }, function(e){throw e;});
    
      return auth.authenticate(loginRequest.username, loginRequest.password, boundAuthenticateFunction);
    });
    

    请注意,上面的代码示例未经测试...

    【讨论】:

      猜你喜欢
      • 2013-11-13
      • 2012-07-26
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2013-07-30
      • 2016-11-27
      • 2015-03-28
      • 2010-11-22
      相关资源
      最近更新 更多