What does passport.session() middleware do?

前端 未结 4 1691
小鲜肉
小鲜肉 2020-12-04 05:58

I am building an authentication system using Passport.js using Easy Node Authentication: Setup and Local tutorial.

I am confused about what passport.session()<

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 06:08

    From the documentation

    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.

    and

    Sessions

    In a typical web application, the credentials used to authenticate a user will only be transmitted during the login request. If authentication succeeds, a session will be established and maintained via a cookie set in the user's browser.

    Each subsequent request will not contain credentials, but rather the unique cookie that identifies the session. In order to support login sessions, Passport will serialize and deserialize user instances to and from the session.

    and

    Note that enabling session support is entirely optional, though it is recommended for most applications. If enabled, be sure to use express.session() before passport.session() to ensure that the login session is restored in the correct order.

提交回复
热议问题