[英]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.