Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor.js: Get anonymous visitors unique ID/ip/whatever?

Tags:

meteor

Let's say I'm building an app using meteor.js where I just collect some simple form data from users. Maybe an answer to a simple question or something. They don't need to log in to submit data.

How can I protect my app from someone creating a js-loop in their Chrome Console that just inserts crap into my DB?


I can protect removal and updates by doing this:

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

And if the user was logged in (which as you remember is not the case in my app) I could timestamp each insert and check something like:

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

So my question is: is there any other way of getting a unique userId-thing or IP-address or something for an anonymous (not logged in) user so I can do the above check on him as well?

Thanks!

like image 699
Kristoffer K Avatar asked Feb 07 '13 16:02

Kristoffer K


2 Answers

You can use a meteorite package.

accounts-anonymous

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

So you use

Meteor.loginAnonymously();

if the user visits your page for the first time, and use .allow to check what you need

like image 138
Suburbio Avatar answered Sep 22 '22 08:09

Suburbio


To get the ip address, the observatory (https://github.com/jhoxray/observatory) project uses this:

in coffee:

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

Which returns an object like { forwardedFor: '192.168.5.4', remoteAddress: '192.168.5.4' }

like image 40
Tarang Avatar answered Sep 20 '22 08:09

Tarang