使用Node.js验证DDP客户端

时间:2014-09-12 19:56:31

标签: javascript node.js meteor ddp

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})
    }
})

1 个答案:

答案 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})
    }
})