Denis Gorbachev
Denis Gorbachev

Reputation: 507

Accessing Meteor application as another user

I've recently updated some parts of the code and want to check if they play well with production database, which has different data sets for different users. But I can only access the application as my own user.

How to see the Meteor application through the eyes of another user?

Upvotes: 2

Views: 554

Answers (3)

phocks
phocks

Reputation: 3273

Slightly updated answer from the accepted to log the client in as new user as well as on the server.

logmein: function(user_id_to_log_in_as) {
  if (Meteor.isServer) {
    this.setUserId(user_id_to_log_in_as);
  }
  if (Meteor.isClient) {
    Meteor.connection.setUserId(user_id_to_log_in_as);
  }
},

More info here: http://docs.meteor.com/api/methods.html#DDPCommon-MethodInvocation-setUserId

Upvotes: 0

Tarang
Tarang

Reputation: 75945

UPDATE: The best way to do this is to use a method

Server side

Meteor.methods({
    logmein: function(user_id_to_log_in_as) {
        this.setUserId(user_id_to_log_in_as);
    }
}):

Client side

Meteor.call("logmein", "<some user_id of who you want to be>");

This is kept simple for sake of clarity, feel free to place in your own security measures.

Upvotes: 2

Xyand
Xyand

Reputation: 4488

I wrote a blog post about it. But here are the details:

On the server. Add a method that only an admin can call that would change the currently logged user programatically:

Meteor.methods(
  "switchUser": (username) ->
    user = Meteor.users.findOne("username": username)
    if user
      idUser = user["_id"]
      this.setUserId(idUser)
      return idUser
)

On the client. Call this method with the desired username and override the user on the client as well:

Meteor.call("switchUser", "usernameNew", function(idUser) {
    Meteor.userId = function() { return idUser;};
});

Refresh client to undo.

This may not be a very elegant solution but it does the trick.

Upvotes: 2

Related Questions