Meteor setUserId on both client and server

I’m trying to impersonate a user on my app (I looked at some packages but they weren’t what I needed).
I read on the Meteor documentation ( that I can use the setUserId method to set the userId I want.
I tried that but it only work on the client side when I use Meteor.connection.setUserId(userId)
When I try to use it on the server (this.setUserId(userId)) I get an exception -

Object [object global] has no method ‘setUserId’

On the client side I used-

function impersonateUser(userId){'impersonateUser', userId,  function(err,res){
        if (!err) {

And on the server side I used-

‘impersonateUser’ : (userId)=>{
if (!Meteor.userId()) {
throw new Meteor.Error(‘impersonateUser.unauthorized’,
‘Cannot merge impersonate user if not connected’);
if(Meteor.users.find({_id: userId}, {limit: 1}).count() == 0){
throw new Meteor.Error(‘impersonateUser.userDoesntExist’,
‘Cannot impersonate user if not exist’);

I need to set the user on both client and server side to get what I need.
Can anybody help me with that?


Hey, you need to do this.setUserId(userId) inside of a meteor method. Where did you try to run it? Why don’t you show some code, that might help to help you.

Hi @tomsp,
I edited the post and added the code I’m using.
Thank you

ok, include a console.log(this) call in your method and see what kind of object this is. then, try defining the impersonateUser method without the fat-arrow function and run it again. anything?

1 Like

Hi @tomsp,
It was the fat-arrow function as you suggested - I changed it to regular function.
Thank you!

1 Like

Another question,
After I set the userId, can I get the previous userId (the one who’s impersonating the new user)?

You’d have to store it somewhere first before setting the userId

Is that the only way?
When I refresh the page the user goes back to the original user so I guess that Meteor saves the original user somewhere

try using:


1 Like

@shaile thank you!
It worked