What is a .initialize () passport? (nodejs express)

I'm trying to apply a passport module in my applications.

I read some manuals and they say there

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

What exactly is doing app.use(passport.initialize())?

passport.session() maybe for a passport to use session information,

But I have no idea about passport.initialize()

+13
source share
3 answers

passport.initialize()is the middleware that initializes Passport .

Middleware is functions that have access to the request object (req), the response object (res), and the next middleware function in the application's request-response cycle.

Passport - Node, .

, passport.initialize() .

passport.session() - , "user", ( cookie ), . .

+15

Passportjs:

Connect Express, passport.initialize() Passport . , pass.session() .

, , past.initialize() .
, .

+4

Sometimes it’s better to look into the code: passport github on initialize ()

TL DR

In sessions, initialize()configures functions for serializing / deserializing user data from a query.

You are not required to use passport.initialize()if you are not using sessions.

/**
 * Passport initialization.
 *
 * Intializes Passport for incoming requests, allowing authentication strategies
 * to be applied.
 *
 * If sessions are being utilized, applications must set up Passport with
 * functions to serialize a user into and out of a session.  For example, a
 * common pattern is to serialize just the user ID into the session (due to the
 * fact that it is desirable to store the minimum amount of data in a session).
 * When a subsequent request arrives for the session, the full User object can
 * be loaded from the database by ID.
 *
 * Note that additional middleware is required to persist login state, so we
 * must use the 'connect.session()' middleware _before_ 'passport.initialize()'.
 *
 * If sessions are being used, this middleware must be in use by the
 * Connect/Express application for Passport to operate.  If the application is
 * entirely stateless (not using sessions), this middleware is not necessary,
 * but its use will not have any adverse impact.
...
+2
source

Source: https://habr.com/ru/post/1687141/


All Articles