E
E
ennet2016-07-14 14:23:55
JavaScript
ennet, 2016-07-14 14:23:55

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
});

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);
};

passportjs
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
What could be the problem? Where can I see more details? Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
poshlipismo, 2019-05-24
@poshlipismo

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 question

Ask a Question

731 491 924 answers to any question