[英]Meteor: check email does not exist in Meteor.users collection before creating an account
我有一个表单,用户将其电子邮件地址和密码输入到联接表单中。 这创建了帐户,但我现在想进一步开发它。
client.js:
Template.joinForm.events({
'submit .form-join': function(e, t) {
e.preventDefault();
var email = t.find('#email').value,
password = t.find('#password').value,
username = Random.id(),
array = [],
profile = {
nameOfArray: array
};
Accounts.createUser({
email: email,
username: username,
password: password,
profile: profile
}, function(error) {
if (error) {
alert(error);
} else {
Router.go('/');
}
});
}
});
在创建用户帐户之前 ,您如何:
检查Meteor.users
集合中是否已存在来自joinForm的email
变量。 在服务器上处理这个?
如果确实存在email
,那么拒绝用户创建?
我看过新功能,想知道我是否可以使用这个http://docs.meteor.com/#/full/accounts_validatenewuser
Accounts.validateNewUser(function (user) {
// pseudo if statement code
if (email not exist) {
// 1. create the user account and then
Accounts.sendVerificationEmail(userId, [email])
} else {
throw new Meteor.Error(403, "email address is already registered");
}
});
谢谢您阅读此篇。
我不清楚是否使用Accounts.createUser
或Accounts.onCreateUser
以及哪些代码应该在客户端上 ,以及哪些代码应该在服务器上 。 我的目标是安全地构建帐户,因此,在此过程中从控制台拒绝任何其他修改权限。
现在, 如果允许创建帐户,即传递validateNewUser
函数,则在服务器上创建额外的空数组nameOfArray
。 当然,您可以添加更多验证检查,例如密码长度。
client.js:
Template.joinForm.events({
'submit .form-join': function(e, t) {
e.preventDefault();
var email = t.find('#email').value,
password = t.find('#password').value,
username = Random.id();
Accounts.createUser({
email: email,
username: username,
password: password,
profile: profile
}, function(error) {
if (error) {
alert(error.reason);
} else {
Router.go('/');
}
});
}
});
server.js:
Accounts.onCreateUser(function(options, user) {
var newEmail = user.emails[0].address;
console.log(newEmail);
var emailAlreadyExist = Meteor.users
.find({"emails.address": newEmail}, {limit: 1})
.count() > 0;
console.log(emailAlreadyExist + ' already exists');
if (emailAlreadyExist === true) {
throw new Meteor.Error(403, "email already registered");
} else {
profile = options.profile;
profile.nameOfArray = [];
user.profile = profile;
return user;
}
});
我发现Accounts.createUser
内置了自己的验证并检查现有的电子邮件/登录。
Meteor docs:Accounts.createUser:
如果现有用户的用户名或电子邮件只是大小写不同,则createUser将失败。
因此Accounts.onCreateUser
如果甚至不火Accounts.createUser
电子邮件/登录验证抛出错误。
Accounts.validateNewUser
函数要求用户在提交后验证其电子邮件。 基本上,在您注册某些内容之后,在您登录之前必须输入通过电子邮件或移动设备发送给您的代码 - 基本上,它确保用户是他们所说的人。 这可能会阻止您注册电子邮件至totallyfake@totally_not_a_real_place.com
。
如果我正确地阅读您的问题,您更感兴趣的是查看电子邮件是否唯一,而不是查看用户是否实际拥有该电子邮件帐户。 您可以使用在服务器上运行的Accounts.onCreateUser
执行此操作:
每当创建新用户时调用。 返回新的用户对象,或抛出错误以中止创建。
整个过程看起来像这样。 在客户端,正是你拥有的:
Template.joinForm.events({
'submit .form-join': function(e, t) {
e.preventDefault();
var email = t.find('#email').value,
password = t.find('#password').value,
username = Random.id(),
array = [],
profile = {
nameOfArray: array
};
Accounts.createUser({
email: email,
username: username,
password: password,
profile: profile
}, function(error) {
if (error) {
alert(error);
} else {
Router.go('/');
}
});
}
});
然后,在服务器上,在实际创建用户之前,它将通过onCreateUser函数运行用户,并且您返回的任何内容都将插入到users集合中:
Accounts.onCreateUser(function(options, user) {
var email = user.emails[0];
if (!email) { throw new Meteor.Error("You must provide a non-empty email"); // this may actually not be necessary -- createUser might do it for you
if (Meteor.users.find({emails: email}) {
throw new Meteor.Error("A user with email " + email + " already exists!");
}
... // whatever other transformation you might want
return user;
});
您也可以查看accounts-ui包 ,因为根据您想要做多少而不同于用户帐户的vanilla实现,可能已经为您完成了很多工作。
Accounts.validateNewUser用于检查用户对象的字段是否符合所需格式,相应地返回true或false。
要检查电子邮件是否已注册,我认为您应该在Accounts.onCreateUser函数( http://docs.meteor.com/#/full/accounts_oncreateuser )中包含此验证。
没有测试代码,您可以尝试这样的事情:
Accounts.validateNewUser(function (user) {
// Check compliance of the user object fields, using check http://docs.meteor.com/#/full/check
});
Accounts.onCreateUser(function(options, user) {
if (options.profile){
user.profile = options.profile;
}
if (Meteor.users.find({email: user.email}).fetch == 0) {
if(Accounts.validateNewUser(user)){
Accounts.sendVerificationEmail(userId, [email]);
return user;
} else {
throw new Meteor.Error(403, "Error checking user fields");
} else {
throw new Meteor.Error(403, "email address is already registered");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.