Hi,
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 (http://docs.meteor.com/#/full/method_setUserId) 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){
smCtrl.call('impersonateUser', userId, function(err,res){
if (!err) {
Meteor.connection.setUserId(userId);
}
else{
alert(err);
}
});
}
And on the server side I used-
Meteor.methods({
‘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’);
}
this.setUserId(userId);
}
});
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.
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?