Get current user id on server side outside publish


#1

Hi,

Maybe this very strange question, I would like to get current logged in user ID outside publish method.
Reason is I want to connect two Remote collection is user is on paid plan example:

if (user.isPaid) { //use normal mongo connection Items = new Mongo.Collection("items", {idGeneration: 'MONGO'}); } else { //user different connection let dbConn2 = "mongodb://user:pass@mymongo.com/dbname"; let database2 = new MongoInternals.RemoteCollectionDriver(dbConn2); Items = new Mongo.Collection("items", {idGeneration: 'MONGO', _driver: database2}); }

So, to initialise this collection on server side, I need to know current user ID.

Any help much appreciated!


#2

Just use Meteor.userId(), as referenced here: http://docs.meteor.com/#/full/meteor_userid


#3

But Meteor.userId() is not available on server side, that’s the issue.
and this.userId is only available inside publish.


#4

Who claims that?

If you actually followed the link I provided for you, you’d read that you can use Meteor.userId()

Anywhere but publish functions

Which means both client and server.

Reading the docs is a highly important step to learn Meteor, I recommend it together with the Meteor guide http://guide.meteor.com.


#5

Just tried:

if (Meteor.isServer) { console.log(Meteor.userId()); }

and I get this:

Meteor.userId can only be invoked in method calls. Use this.userId in publish functions.


#6

Now, the question is, what is the reason that you call your function outside of methods.

Further from the guide:

On the server, each connection has a different logged in user, so
there is no global logged-in user state by definition. Since Meteor
tracks the environment for each Method call, you can still use the Meteor.userId()
global, which returns a different value depending on which Method you
call it from, but you can run into edge cases when dealing with
asynchronous code. Also, Meteor.userId() won’t work inside publications.

We suggest using the this.userId property on the context
of Methods and publications instead, and passing that around through
function arguments to wherever you need it.

So you can also pass the userID from the publication or method, where you get it either as Meteor.userId() or this.userId().


#7

But yeah, @sashko the docs are not precise here, they state we can use Meteor.userId() anywhere but publications, while the guide says we actually can’t.

@pinku1 if you don’t want to pass the Id from the method or publication for some reason, you could also try Meteor.users()._id, it may work.


#8

I think I found solution,
Actually I changed my approach, i created another collection called ItemsNew which connects to remote db.

Also, Meteor.users()._id does not work.

Many thanks @brajt for your support, actually here is link to product I am working on http://www.stackable.space/

Please feel free to give your feedback if you get time to look into it.
It’s still in development bit early stages.


#9

Lol, I meant Meteor.user()._id, not users. But I guess if Meteor.userId() doesn’t work, this one won’t work too.

Anyways, I’m glad it’s working now with new approach. If it was me, I’d probably call your function in user publication aand pass this.userId(), but whatever works is fine.


#11

I have the same issue where I have a separate Mongo Database per user. To know the name of the Database I connect to I have to check the user collection. How did you solve this? Do you still need the user in your new approach or did you find a way around this?


#12

Have you tried using this.userId in server code? this is available only when user is logged in.


#13

Yes this.userId only works in Methods and Meteor.userId() in publish. Outside publish and meteor methods there is no way to tell what user you are. The reason is because the Server has no per user state tracking. Methods and Publish functions do because they use the DDP Protocol and get called from User requests.