javascript 什么是passport.initialize()?(nodejs 快递)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46644366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 06:50:08  来源:igfitidea点击:

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

javascriptnode.jsnpmpassport.jsnode-modules

提问by jwkoo

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?

什么是app.use(passport.initialize())究竟在做什么?

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

passport.session()可能是护照使用会话信息,

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

但我不知道 passport.initialize()

回答by Harikrishnan

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

passport.initialize()是一个初始化Passport的中间件。

Middlewaresare 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.

中间件是可以访问请求对象 (req)、响应对象 (res) 和应用程序请求-响应循环中的下一个中间件函数的函数。

Passport is an authentication middleware for Node that authenticates requests.

Passport 是 Node 的身份验证中间件,用于对请求进行身份验证。

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

所以基本上passport.initialize()初始化认证模块。

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.

passport.session()是另一个中间件,它更改请求对象并将当前会话 ID(来自客户端 cookie)的“用户”值更改为真正的反序列化用户对象。在这里详细解释。

回答by jpenna

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

有时最好查看代码:initialize() 上的passport github

TL;DR

TL; 博士

With sessions, initialize()setups the functions to serialize/deserialize the user data from the request.

使用会话,initialize()设置函数以序列化/反序列化请求中的用户数据。

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

不需要使用passport.initialize(),如果你不使用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.
...

回答by TGrif

From the Passportjs documentation:

从 Passportjs 文档:

In a Connect or Express-based application, passport.initialize() middleware is required to initialize Passport. If your application uses persistent login sessions, passport.session() middleware must also be used.

在基于 Connect 或 Express 的应用程序中,需要passport.initialize() 中间件来初始化Passport。如果您的应用程序使用持久登录会话,则还必须使用passport.session() 中间件。

If we have a look at the source code, we can see that passport.initialize() middleware basically add passport instance to incoming requests so that authentication strategy can be proceed.
If there is a session, it is added to requests as well.

如果我们查看源代码,我们可以看到passport.initialize() 中间件基本上将passport 实例添加到传入请求中,以便可以进行身份​​验证策略。
如果有会话,它也会被添加到请求中。