What does passport.session() middleware do?

前端 未结 4 1675
小鲜肉
小鲜肉 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:05

    While you will be using PassportJs for validating the user as part of your login URL, you still need some mechanism to store this user information in the session and retrieve it with every subsequent request (i.e. serialize/deserialize the user).

    So in effect, you are authenticating the user with every request, even though this authentication needn't look up a database or oauth as in the login response. So passport will treat session authentication also as yet another authentication strategy.

    And to use this strategy - which is named session, just use a simple shortcut - app.use(passport.session()). Also note that this particular strategy will want you to implement serialize and deserialize functions for obvious reasons.

    0 讨论(0)
  • 2020-12-04 06:06

    passport.session() acts as a middleware to alter the req object and change the 'user' value that is currently the session id (from the client cookie) into the true deserialized user object.

    Whilst the other answers make some good points I thought that some more specific detail could be provided.

    app.use(passport.session());
    

    is equivalent to

    app.use(passport.authenticate('session'));
    

    Where 'session' refers to the following strategy that is bundled with passportJS.

    https://github.com/jaredhanson/passport/blob/master/lib/strategies/session.js

    Specifically lines 59-60:

    var property = req._passport.instance._userProperty || 'user';
    req[property] = user;
    

    Where it essentially acts as a middleware and alters the value of the 'user' property in the req object to contain the deserialized identity of the user. To allow this to work correctly you must include serializeUser and deserializeUser functions in your custom code.

    passport.serializeUser(function (user, done) {
        done(null, user.id);
    });
    
    passport.deserializeUser(function (user, done) {
        //If using Mongoose with MongoDB; if other you will need JS specific to that schema.
        User.findById(user.id, function (err, user) {
            done(err, user);
        });
    });
    

    This will find the correct user from the database and pass it as a closure variable into the callback done(err,user); so the above code in the passport.session() can replace the 'user' value in the req object and pass on to the next middleware in the pile.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-04 06:11

    It simply authenticates the session (which is populated by express.session()). It is equivalent to:

    passport.authenticate('session');
    

    as can be seen in the code here:

    https://github.com/jaredhanson/passport/blob/42ff63c/lib/authenticator.js#L233

    0 讨论(0)
提交回复
热议问题