nodejs session working locally but not on a remote server

大兔子大兔子 提交于 2019-12-12 20:38:53

问题


I am not able to do a sign up and login using passort.js and express.js, locally this works very fine but when I deploy the app on a remote server the login fails to redirect me after logging. It does not even flash the failure messages.

I also noted that when I do a signup on the remote server it works fine but does not flash message. req.isAuthenticated() always returns false. I am using MongoDB as my datasource

app.use(session({secret : '3T67774A-R649-4D44-9735-43E296ZZ980F', saveUninitialized: true, resave: true}));
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session

function isLoggedIn(req, res, next) {
    console.log("Request Authenticated: " + req.isAuthenticated());
    if (req.isAuthenticated())
        return next();
    res.redirect('/login');
}

passport.use('local-login', new LocalStrategy({
    // by default, local strategy uses username and password, we will override with email
    usernameField : 'email',
    passwordField : 'password',
    passReqToCallback : true // allows us to pass in the req from our router (lets us check that a use is logged in or not
}, function(req, email, password, done) {
    if (email)
        email.toLowerCase(); // use lower-case emails to avoid case-sensitive email matching

    // asynchronous
    process.nextTick(function() {
        User.findOne({'local.email' : email}, function(err, user) {
            // if there are any errors, return the error
            if (err) 
                return done(err);

            // if no user found, return the message
            if (!user)
                return done(null, false, req.flash('loginMessage', 'User not found.'));

            if (!user.isValidPassword(password))
                return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.'));

            // return user
            else
                return done(null, user);
        });
    });
}));

来源:https://stackoverflow.com/questions/36678597/nodejs-session-working-locally-but-not-on-a-remote-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!