我似乎无法理解Accounts.createUser()和Accounts.onCreateUser()之间的关系。我有一个验证用户登录凭证的外部API。一旦API向我发送了积极的响应,我需要将用户添加到MongoDB中并开始其会话,以便它可以被认为是登录用户的。 Accounts.createUser()正在服务器端创建一个用户,但我需要Accounts.onCreateUser(),因为我需要添加自定义字段,如从外部API生成的用户令牌。在服务器端手动添加用户并设置他们的会话
这是代码,我现在所拥有的(不添加用户的话):
服务器端代码:
var request = {
'headers': {
'Content-Type': 'application/x-www-form-urlencoded'
},
'params': user
};
try {
var response = HTTP.call('POST', url, request); //send call to the external api
var token = response.data.token;
//decode the token and add the user in the database
var userInfo = Base64.decode(token.split('.')[1]);
var options = {
email: user._username,
profile: {
name: user._username
},
token: token
};
var user = Accounts.onCreateUser(function(options, user) {
if (options.token)
user.token = options.token;
if (options.profile)
user.profile = options.profile;
return user;
});
console.log(user); //this returns undefined
return JSON.stringify({
'code': 200,
'token': userInfo
});
} catch (error) {
console.log(error);
//console.log(error.response);
var body = error.response.content;
return body;
}