Session does not persist using express-Session and Passport.js

Asked

Viewed 119 times

0

When logging in, my user is redirected to another router, only the session is not saved:

router.post('/login', passport.authenticate('delegados', { failureRedirect: '/login', failureFlash: true }), (req, res, next) => {
  req.session.save((err) => {
    if (err) {
      console.log(err);
      return next(err);
    }
    console.log(req.session);
    console.log(req.user);
    res.redirect('/participante');
  });
});

Login:

app.use(cookieParser('secretkey'));
app.use(session({ name: 'sessionid',
  secret: 'secretkey',
  resave: true,
  saveUninitialized: true,
  store: new MongoStore({ mongooseConnection: mongoose.connection }),
  cookie: { secure: false } }));
app.use(passport.initialize());
app.use(flash());
app.use(passport.session());

Router to which it is redirected:

router.get('/participante', function (req, res) {
  console.log(req.user);
  if(!req.user){
    return res.redirect('/login');
  }
  res.render('participante', {user: req.user});
});

Logs in the order shown in the code:

GET /login 304 3.906 ms - -
GET /stylesheets/style.css 304 4.334 ms - -
GET /images/logo.png 304 7.702 ms - -
GET /images/logo.png 200 8.145 ms - 38664
Session {
  cookie:
   { path: '/',
     _expires: null,
     originalMaxAge: null,
     httpOnly: true },
  flash: {},
  passport: { user: [Function] } }
{ _id: 5c52608443deb33bd40ae322,
  nome: 'Nome',
  email: '[email protected]',
  matricula: 2019001,
  comite: 'press',
  pais: null,
  salt:
   'salt',
  hash:
   'hash',
  __v: 0 }
POST /login 302 429.967 ms - 70
undefined
GET /participante 302 2.513 ms - 56
GET /login 200 3.427 ms - 4988
  • Hello, the code snippet where you set the settings of Session, could you post? You set the setting to be able to save the Session again?

  • @Justcase edited the post to add that part. Grateful!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.