流星的节点光纤故障



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

我认为问题在于我创建的光纤,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()};
        }
    });
});

我认为问题是与需要使用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);
});

注意,上面的代码示例是未经测试的…

相关内容

  • 没有找到相关文章