I have a nodejs + express project with passportjs authorization.

If I log in to my local host (localhost: 3000), then req.isAuthenticated returns true and the user session is working correctly.

but if you do the same thing on the server dev, then the session drops, since req.isAuthenticated will return false .

We tried to look inside the modules, everything seems to work out correctly. The code is the same and so, the versions of the modules used are also.

Here is a piece of code:

express-session.js

 var session = require('express-session'), express = require('express'), RedisStore = require('connect-redis')(session); var date = new Date; date.setDate(date.getDate() + 30); module.exports = session({ secret: 'SECRET', resave: true, //don't save session if unmodified saveUninitialized: true, // create session until something stored expires: date.toUTCString(), cookie: { secure: false, httpOnly : true, maxAge : 30 * 60 * 1000 //30 minutes }, rolling: true }); введите сюда код 

login.js

 exports.post = function (req, res, next) { passport.authenticate('local', { badRequestMessage: 'Error in your fields' }, function (err, user, info) { if (err) return next(err); if (user) { req.logIn(user, function (err) { if (err) return next(err); return res.format({ json: function () { res.json({ link : '/personal/cabinet/' }); } }); }); } else { return res.format({ html: function () { res.render('auth/login', { error: info.message, title: 'Login' }); }, json: function () { res.json(info); } }); } } )(req, res, next); }; 

pasportjs

 var passport = require('passport'), localStrategy = require('./strategy/localStrategy'), models = require('models'), Author = models.author; //get user data for session passport.serializeUser(function (user, done) { done(null, user); }); passport.deserializeUser(function (user, done) { Author.findOne({ where: { id: user.id } }).then(function (user) { done(null, user); }); }); localStrategy(passport); module.exports = passport.initialize(); 

app.js

 ... app.use('bodyParserJson'); app.use('bodyParserUrldecoded'); app.use('cookieParser'); app.use(path.join('authPassport', 'session')); //express-session app.use(path.join('authPassport', 'init')); //pasportjs app.use(passport.session()); ... 

Versions: passport: 0.3.2, express-session: 1.13.0, express: 4.13.1, nodejs: 4.2.6

Tell me what could be the problem? Where to look more detailed? Thank!

  • So, what about the user you are trying to log in to on both servers? - pitersky
  • yes it exists, it’s clear from requests that it finds the user, authorizes it, then req.isAuthenticated returns true and flies after the redirect - ennet
  • If you haven't figured it out yet: do: console.log (req, req._passport, req._passport.instance._userProperty) before calling req.isAuthenticated from two servers and compare. You can try to put on pastebin, so we also looked - pitersky

0