node.js DDP客户端(使用node-ddp)调用DDP服务器上的方法insertMessage
,该方法将文档保存到mongodb。
Meteor.methods({
'insertMessage': function(msg) {
Messages.insert({'msg':msg, 'userId': userId})
}
})
我们如何才允许经过身份验证的DDP客户端插入包含其唯一标识符userId
的文档,而无法伪造其他人的userId
?我看了ddp-login,但似乎成功的身份验证提供了一个令牌,这个令牌可以用于我们的目的吗?
Meteor.methods({
'insertMessage': function(msg) {
// Check that the current user's userId (how can we do this?)
userId = getUserId()
Messages.insert({'msg':msg, 'userId': userId})
}
})
答案 0 :(得分:4)
在服务器中,你有这个参数..
Meteor.methods
this.userId
this.setUserId
this.isSimulation
this.unblock
this.connection
Meteor.methods({
'insertMessage': function(msg) {
userId = this.userId;
Messages.insert({'msg':msg, 'userId': userId})
}
})