Jeanluca Scaljeri
Jeanluca Scaljeri

Reputation: 29109

How to set Meteor.user()

My meteor app uses github authentication. After a user has authenticated

Meteor.user()

return the current authenticated user (on the client and server).

What I now need is to run my application without authentication. But Meteor.user() should return some user because my code expects it. Is there anyway I can tell my meteor app what Meteor.user() should return ?

Upvotes: 0

Views: 293

Answers (2)

Tomasz Lenarcik
Tomasz Lenarcik

Reputation: 4880

I am not sure what exactly you are trying to achieve, but maybe creating a custom login handle for anonymous user will help. Take a look here for a very nice explanation by Arunoda on extending Meteor's account system.

Though, I think that the best way to go would be to make your code independent on fact the Meteor.user() might be null'ish as @fritzi2000 suggested.

Please note that Meteor.user() may potentially return null even if the user is actually logged in and Meteor.userId() is set properly. This is because Meteor.user() is more or less equivalent to

Meteor.users.findOne({_id: Meteor.userId()});

so it relies on some particular data being fetched from the server. Hence, it's always better to verify the logged-in status with Meteor.userId().

Upvotes: 1

tomet
tomet

Reputation: 2556

Meteor.user() always returns the current user. If there is no user logged in, then Meteor.user() returns null. You cannot change that behavior.

The thing to do then is probably modify your app code in such a way that different code is run when no user is logged in. You can do that like so:

if(Meteor.user()){
    //do stuff when user is logged in
}
else{
    //do stuff if user is not logged in
}

Upvotes: 1

Related Questions