Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Validation Meteor-Account.createUser在客户端和服务器中_Validation_Meteor - Fatal编程技术网

Validation Meteor-Account.createUser在客户端和服务器中

Validation Meteor-Account.createUser在客户端和服务器中,validation,meteor,Validation,Meteor,我理解在客户机和服务器中同时使用业务逻辑的原因,但我不太明白在某些情况下如何做到这一点。例如: //client/client.js //在“创建帐户”按钮上单击事件 Template.homecontent.events({ “单击#btnCreateAccount”:函数(事件、模板){ var userEmail=template.find('#email').value, userName=template.find('#newusername').value, password=tem

我理解在客户机和服务器中同时使用业务逻辑的原因,但我不太明白在某些情况下如何做到这一点。例如:

//client/client.js
//在“创建帐户”按钮上单击事件
Template.homecontent.events({
“单击#btnCreateAccount”:函数(事件、模板){
var userEmail=template.find('#email').value,
userName=template.find('#newusername').value,
password=template.find('#newpassword').value,
password2=template.find('#password2').value,
name=template.find('#fullname').value;
验证=真;
//在这里做一些验证
如果(密码!=密码2){
验证=假;
}
如果(验证===真){
Accounts.createUser({
用户名:用户名,
电子邮件:userEmail,
密码:密码,
简介:{
姓名:姓名
}
},函数(错误){
如果(错误){
console.log(“无法创建用户”);
}
});
}
}
});
由于验证仅在客户机上进行,因此可以轻松绕过验证。
但这里有一个问题:这是由用户事件触发的,所以我不确定在客户端和服务器上运行此代码的最佳方式是什么。

您可能正在寻找类似于
Meteor.methods()
,它允许您在服务器上定义客户端可以使用
Meteor.call()
调用的函数。您可以在服务器上提供一个验证函数和一个用户保存函数,并从客户端调用它们,传递表单数据

我过去所做的是(在客户机上)我有一个
userFormParse()
函数,它接受表单对象并将其解析为可以传递到服务器端验证函数的对象。我对用户编辑和创建表单使用相同的userFormParse函数

validation函数向客户端返回一个错误对象,或者,如果它都是有效数据,我将把数据对象传递给
userCreateWithRole
函数(我通常将角色分配给用户)

在服务器上:

Meteor.methods({
“createUserWithRole”:函数(数据、角色){
var用户id;
Meteor.call('createUserNoRole',数据,函数(err,result){
如果(错误){
返回错误;
}
Roles.addUsersToRoles(结果,角色);
返回userId=result;
});
返回用户标识;
},
“createUserNoRole”:函数(数据){
//执行服务器端验证
returnaccounts.createUser({
电子邮件:data.email,
密码:data.password,
profile:data.profile
});
}
});
然后在客户机上:

Template.userSignup.events({
“提交#用户注册”:函数(事件){
var数据、验证错误;
event.preventDefault();
data=userInputParse($(event.target));//此函数将表单解析为可插入的用户对象
validationErrors=userObjectValidate(data);//此函数对用户对象进行客户端验证。
data.profile.status=0;
如果(验证错误){
//向用户显示验证错误
}否则{
return Meteor.call('createUserWithRole',data',standard',function(err,userId){
如果(!err){
//用户创建!!
}否则{
//插入错误
}
});
}
}
});

该代码是概念性的,未经测试:)

您应该在服务器端使用它,前面的答案并不准确

例如,创建和使用Meteor方法不会阻止用户从控制台调用
Accounts.createUser
。因此,您还需要防止在客户端上创建用户:

Accounts.config({
  forbidClientAccountCreation : true
});
你可能想调查一下

示例(摘自文档):

Accounts.validateNewUser(函数(用户){
如果(user.username&&user.username.length>=3)
返回true;
抛出新Meteor.Error(403,“用户名必须至少有3个字符”);
});

Patrick,你有没有机会分享这个函数的代码?我只是想自己做一个。Ta.这将通过网络发送普通密码,您应该按照@Andrey建议的方式执行,并使用Accounts.onCreateUser()在服务器端添加验证。检查这个问题,alredy用服务器/客户端代码回答了这个问题,创建了具有角色的用户E,实际上我在我的web上使用该代码创建用户希望它有所帮助