How can I log in as a specific user from the meteor shell

395 views Asked by At

I am working on an Meteor application. I have the meteor shell open. I need to do an insert for a quick test, but I need to pretend that I am authenticated as a particular user (I know the userId). Inside the meteor shell, is there a way for me to log in as a particular user given the userId?

Thank you!

2

There are 2 answers

0
Ramil Muratov On

You can't simulate logged in user in meteor shell.

I am not sure what exactly are you testing, but if it is some allow/deny rules on collection, try to create method that do your stuff, log in as wanted user and call method.

Also inside method you can run this.setUserId(userId). This simply sets the value of userId for future method calls received on this connection.

0
maxko87 On

meteor shell gives you access to the server, which is not tied to a particular user session. Instead, you'll want to impersonate users from the client (open the console in your browser).

Try this on the client:

@impersonateUser = (userId) -> Meteor.call "impersonate", userId, (err,res) -> if err console.log err else Meteor.connection.setUserId res._id console.log 'Now impersonating ' + res.emails[0].address

And this on the server:

Meteor.methods impersonate: (query) -> throw new Meteor.Error(403, "Permission denied") unless Meteor.user().isAdmin u = Meteor.users.findOne(query) throw new Meteor.Error(404, "User not found") unless u @setUserId u._id return u

And then run impersonateUser('someUserId') on the client.