Phorce
Phorce

Reputation: 4642

Nodejs - Passport - Failed to serialize user into session

I am using the following library:

passport-oauth2-password-grant

What I'm trying to do is the following:

  1. Authenticate to the API (username / password)
  2. Get the Access token
  3. Get the user profile from the API
  4. Log the user in using passport

I'm using the following implementation:

PasswordGrantStrategy.prototype.userProfile = function(accessToken, done) {
    return done(null, { real_name: 'Foo Baz', email: '[email protected]' });

}
passport.use(new PasswordGrantStrategy({
    tokenURL: 'http://api.local/oauth/token',
    clientID: "2",
    clientSecret: "",
    grantType: "password",
},
function(accessToken, refreshToken, profile, done) {
  return done(null, profile);
}));

function authenticate() {
    return function(req, res, next) {
        var username = "[email protected]";
        var password = "password";

        passport.authenticate('password-grant', {
            username: username,
            password: password
        })(req, res, next);
    };
}

Login to the API using username/password

It's passing the profile into the callback function for the passport.use(new PasswordGrantStrategy however, I am getting the following error:

Failed to serialize user into session

It should be serializing as I'm passing in a profile and all looks fine. Now does this mean there could be a possible issue with sessions?

Upvotes: 2

Views: 8647

Answers (1)

paqash
paqash

Reputation: 2314

You need to provide serializeUser and deserializeUser method to Passport in order for it to work.

From the official docs:

passport.serializeUser(function(user, done) {
  done(null, user.id);
});

passport.deserializeUser(function(id, done) {
  User.findById(id, function(err, user) {
    done(err, user);
  });
});

Upvotes: 6

Related Questions