我正在尝试为 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()};
}
});
});