Answer the question
In order to leave comments, you need to log in
Why does req.isAuthenticated from passposrtjs return false on dev server?
I have a nodejs + express project with passportjs authorization.
If I log in to my localhost (localhost:3000) `req.isAuthenticated` returns true and the user session works correctly.
but if you do the same on the dev server, then the session drops, since req.isAuthenticated will return false.
We tried to look inside the modules themselves, everything seems to be working correctly. The code is the same anyway, the versions of the modules used are the same.
Here is part of the 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
});
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);
};
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.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());
...
Answer the question
In order to leave comments, you need to log in
Are you getting the user object in the middleware where do you have req.isAuthenticated?
I can't understand either. The user object is received, which means that all validations were successful.
I receive a cookie, it lies on the client, where it is supposed to lie.
What could it be? Sessions?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question