Jiew Meng
Jiew Meng

Reputation: 88367

PassportJS how to check if user is logged in?

How can I check if the user is logged in in an ExpressJS/PassportJS app? I believe PassportJS will set the session automatically? But what variable does it set?

Upvotes: 3

Views: 4209

Answers (3)

micrub
micrub

Reputation: 738

In connect.js you should check for request.user in your middleware

How to know if user is logged in with passport.js?

Upvotes: 0

vaughan
vaughan

Reputation: 7482

session.passport.user which is set when calling req.logIn and unset when calling req.logOut

sessionStore = new MongoStore # RedisStore or whatever

sessionStore.get data.sessionID, (err, session) ->
  if session.passport.user?
     # Logged in

Upvotes: 2

Alex Westholm
Alex Westholm

Reputation: 898

If you've got it set up correctly, you should be able to see the Set-Cookie line in the HTTP response after POSTing to your session creation route (try curl -v -d "username=whoever&password=supersecret" http://localhost:3000/session/new with the appropriate substitutions for username, password and url).

However, to answer your question directly, you should be seeing connect.sid and remember_token by default. If you're not seeing this set, but can still dump a valid user object in your authentication strategy, verify that you've got the following lines in your app.configure call:

app.use(express.cookieParser());
app.use(express.session({secret: "replace"}); // lots of options here, read up
app.use(passport.session());

Upvotes: 0

Related Questions