我有一个表单,用户将其电子邮件地址和密码输入到联接表单中.这创建了帐户,但我现在想进一步开发它.
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('/');
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
在创建用户帐户之前,您如何:
检查email集合中是否已存在joinForm中的变量 Meteor.users.在服务器上处理这个?
如果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");
}
});
Run Code Online (Sandbox Code Playgroud)
谢谢您阅读此篇.
我不清楚是否要使用Accounts.createUser或者Accounts.onCreateUser哪些代码应该在客户端上,以及服务器上的代码.我的目标是安全地构建帐户,因此,在此过程中从控制台拒绝任何其他修改权限.
nameOfArray现在,如果允许创建帐户,即传递validateNewUser函数,则在服务器上创建额外的空数组.当然,您可以添加更多验证检查,例如密码长度.
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('/');
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
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;
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4402 次 |
| 最近记录: |