Do Express session and Passport session conflict in an Express app? Why or why not?
Here is some code that distinguishes Express and Passport session objects:
app.use(express.session({})); app.use(passport.session()); app.use(session({ cookie : { maxAge : 60000 } }));
Passport is a popular, modular authentication middleware for Node. js applications. With it, authentication can be easily integrated into any Node- and Express-based app. The Passport library provides more than 500 authentication mechanisms, including OAuth, JWT, and simple username and password based authentication.
Express-session - an HTTP server-side framework used to create and manage a session middleware. This tutorial is all about sessions. Thus Express-session library will be the main focus. Cookie-parser - used to parse cookie header to store data on the browser whenever a session is established on the server-side.
Passport uses serializeUser function to persist user data (after successful authentication) into session. The function deserializeUser is used to retrieve user data from session and perform some condition-based operations. Now all the endpoints hitting the backend server will go through passport.
Passport. js provides authentication, not security. It is fairly easy to misconfigure by following online tutorials, so take care - the tool is only as good as the hand it is in.
No, they are two separate things and they do not conflict which other. Moreover, passport.session
has to be used after express.session
in order to work properly.
express.session
middleware is used to retrieve user session from a datastore (like Redis). We can find the session object because the session Id is stored in the cookie, which is provided to the server with every request.
Then, the purpose of passport.session
middleware is to deserialize user object from session using passport.deserializeUser
function (that you define in your passport configuration). When user first authenticates itself, its user object is serialized and stored in the session. On each following request, the middleware deserialize the user and populates req.user
object.
Check Passpot Configure Guide and this SO answer: What does passport.session() middleware do? for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With