Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor.user() on iron-router server side

How can check, on server side route, if user is logged?

I would add check on 'before', but Metor.user() don't work here.

thanks in advance.

p.s. I have found How to get Meteor.user() to return on the server side?, but not work on iron-router

like image 991
elbowz Avatar asked Nov 26 '13 14:11

elbowz


1 Answers

I'm afraid that this is not possible. I guess that the problem comes from the fact that you're trying to connect to the server with two different protocols - both literally and in logically - so there is no obvious way to relate this two actions.

There is, however, a pretty simple solution that may suit your needs. You'll need to develop a simple system of privileges tokens, or secret keys, or whatever you call them. First, create a server method

var Secrets = new Meteor.Collection("secrets"); // only on server!!!

Meteor.methods({
  getSecretKey: function () {
    if (!this.userId)
      // check if the user has privileges
      throw Meteor.Error(403);
    return Secrets.insert({_id: Random.id(), user: this.userId});
  },
});

Then, you can now use it on the client to get the secretKey which attach to your AJAX request (or something), either within the HTTP header or in the URL itself. Fear not! They will all be encrypted if you're using HTTPS.

On the server side you can now retrieve the secretKey from the incoming request and check if it is present in the Secrets collection. You'll know then if the user is granted certain privileges or not. Also you may want to remove your secret keys from the collection after some time for safety reasons.

like image 174
Tomasz Lenarcik Avatar answered Dec 11 '22 09:12

Tomasz Lenarcik