what is passport.initialize()? (nodejs express)

jwkoo picture jwkoo · Oct 9, 2017 · Viewed 13.6k times · Source

I'm now trying to apply passport module in my apps.

I'm reading some manuals, and there say,

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

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

passport.session() is maybe for the passport to use the session information,

But I have no idea about the passport.initialize()

Answer

Harikrishnan picture Harikrishnan · Oct 9, 2017

passport.initialize() is a middle-ware that initialises Passport.

Middlewares are 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 is an authentication middleware for Node that authenticates requests.

So basically passport.initialize() initialises the authentication module.

passport.session() is another middleware that alters the request object and change the 'user' value that is currently the session id (from the client cookie) into the true deserialized user object. It is explained in detail here.