Tuesday, 25 April 2017

PassportJS's serializeUser not setting session

After the user goes through the signup process I call authenticate, which is supposed to call login by default, but I put an extra call to login for good measure to set the session, and still the session isn't storing the serializeUser contents of _id. The end goal is to call req.user to get the currently logged in user, but nothing is returned.

  • I'm running my Angular2 app on localhost:4200, NodeJS server on localhost:8080, and my redis server from port 6379.

  • The user id to user vice versa in serializeUser and deserializeUser is correct.

  • My serializeUser function is called, but my deserializeUser function isn't.

  • My redis server does work and is receiving session strings, but they are formatted incorrectly like below.

Here's my session, it's missing the _id (user id) data.

{"data":{"cookie":{"originalMaxAge":300000,"expires":"2017-04-25T22:43:32.605Z","secure":false,"httpOnly":true,"path":"/"}}}

server.js

...
const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
const session = require("express-session");
var RedisStore = require("connect-redis")(session);

app.use(function(req, res, next){
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
  res.header('Access-Control-Allow-Credentials', "true");
  next();
});

var options = {
  "port": nconf.get("redis:urlPort"),
  "host": nconf.get("redis:urlURI")
};

app.use(session({
  store: new RedisStore(options),
  cookie: {
    secure: false,
    maxAge: 300000
  },
  secret: 'starbucks-sucks',
  resave: true, 
  saveUninitialized: true 
}));

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

server.js: serializeUser & deserializeUser

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

passport.deserializeUser(function(user_id, done) {
  db.collection(USERS_COLLECTION).findOne({_id: new ObjectID(user_id) }, function(err, doc){
    if(err){
      handleError(res, err.message, "Failed to get user");
    } else{
      done(null, doc);
    }
  });
});

server.js: other

passport.use(new LocalStrategy({
  usernameField: "phone",
  passwordField: "auth_code"
},function(username, password, done) {
  db.collection(USERS_COLLECTION).findOne({"phone": username})
    .then(function(data){
      if(data.auth_code === password){ return done(null, data); }
        else{ done(null, false, {message: "Verification code is incorrect."}); }
    }, function(err){
      done(err);
    });
}));

app.post("/login", function(req, res, next) {
  passport.authenticate("local", function (err, user, info) {
    if (err) { next(err); }
    if (!user){ next(err); }

    req.login(user, function(err){
      if(err){ return next(err); }
      res.status(200).json(user);
    });
  })(req, res, next);
});



via Glenn Dayton

No comments:

Post a Comment