Meteor.js:为匿名访问者提供唯一的ID/ip/whatever



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

我如何保护我的应用程序不受在Chrome控制台中创建js循环的人的影响,该循环只会在我的数据库中插入垃圾


我可以通过这样做来保护删除和更新:

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或IP地址,这样我也可以对他进行上述检查?

谢谢!

您可以使用陨石包。

账户匿名

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

所以你使用

Meteor.loginAnonymously();

如果用户第一次访问您的页面,请使用.allow检查您需要的

要获取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' } 的对象

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

您可以这样做:

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

并检查"currentuser"是否已插入到您的数据库中。

最新更新