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
.