Meteor.js:获取匿名访客唯一ID / ip /等等?

时间:2013-02-07 16:06:34

标签: meteor

假设我正在使用meteor.js构建应用程序,我只是从用户那里收集一些简单的表单数据。也许是对一个简单问题的回答。他们无需登录即可提交数据。

如何保护我的应用免受在Chrome控制台中创建js-loop的人员的影响?只需在我的数据库中插入垃圾?


我可以通过以下方式保护删除和更新:

Formanswers.allow({
  insert: function () {
    return true;
  },
  update: function () {
    return false;
  },
  remove: function () {
    return false;
  },
});

如果用户已登录(我记得在我的应用中不是这种情况),我可以为每个插入添加时间戳并检查以下内容:

  insert: function (userId, doc) {
        if (userId && (Formanswers.findOnd({userid: userId, time: SOMETHING TIME SPECIFIC}).count() < 1)) return true;
  },

所以我的问题是:有没有其他方法可以为匿名(未登录)用户获取唯一的userId-thing或IP地址或东西,所以我也可以对他进行上述检查?

谢谢!

4 个答案:

答案 0 :(得分:13)

你可以使用陨石包。

帐户匿名

https://github.com/tmeasday/meteor-accounts-anonymous

所以你使用

Meteor.loginAnonymously();

如果用户第一次访问您的网页,并使用.allow检查您需要的内容

答案 1 :(得分:1)

要获取IP地址,天文台(https://github.com/jhoxray/observatory)项目使用此:

在咖啡中:

Meteor.userIP = (uid)->
  ret = {}
  if uid?
    s = ss for k, ss of Meteor.default_server.sessions when ss.userId is uid
    if s
      ret.forwardedFor = s.socket?.headers?['x-forwarded-for']
      ret.remoteAddress = s.socket?.remoteAddress
  ret

返回像{ forwardedFor: '192.168.5.4', remoteAddress: '192.168.5.4' }

这样的对象

答案 2 :(得分:0)

使用会话或localStorage密钥。当访问者提交表单时,检查是否已设置密钥,如果已设置,则拒绝插入。

答案 3 :(得分:0)

您可以这样做:

if (Meteor.isClient) {
    Meteor.startup(function () {
        Session.set('currentuser', 'something randomly generated by another function');
    }
}

并检查数据库中是否已插入'currentuser'。

相关问题