سؤال

I finally got done replacing an everyauth/ mongoose-auth login system with a passport implementation. I'm using this purely for local username/ password logins, so I'm also utilizing the passport-local module as well.

I know from looking through a few examples, I found Passport auto-assigns a couple req helpers. I've not been able to find a full list of variables it puts there though, a couple I've found are req.isAuthenticated() and the req.user variables.

Is there a full list provided anywhere online? Just interested in my options available in routes/ views. I can keep trolling through examples, but it would be nice if there was a reference somwhere.

هل كانت مفيدة؟

المحلول

For a Connect/Express application,

1.var passport = require('passport');

The following four helper functions are added to http.IncomingMessage.prototype(i.e., the request object's prototype):

  • login/logIn(user, [options,] done)
  • logout/logOut()
  • isAuthenticated() - i.e. whether req.user exists.
  • isUnauthenticated()

If a user is authenticated successfully, usually a callback function done(null, user) is called. This callback function then calls req.logIn() which in turn calls serializeUser() to store the user id as req._passport.session.user.

The req.logOut() function deletes req._passport.session.user.

2.app.use(passport.initialize());

Get the passport info from current session and store it as req._passport.session(i.e., req.session['passport']).

3.app.use(passport.session());

Check whether req._passport.session.user exists, that is, whether the user id is stored in current session. If yes, call deserializeUser() to get the user object which will be stored as req.user.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top