Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS express session expire after page refresh

The session of my nodejs app is expiring every time I refresh the page, after login. It does work fine if I visit different pages but as soon as I refresh the page, the session ends. I've tried a couple of things but none of it seems to work. How can I keep it from expiring even after the page refresh? If I can store session in the database or someplace else to keep it from expiring.

Here are the files

Passport-init.js

 var mongoose = require('mongoose');
 var User = mongoose.model('user');
 var localStrategy = require('passport-local').Strategy;
 var bcrypt = require('bcrypt-nodejs');

module.exports = function(passport) {

passport.serializeUser(function(user, done) {
    console.log('serializing user:',user.username);
    done(null, user._id);
});

passport.deserializeUser(function(id, done) {

    User.findById(id, function(err, user) {

        if(err) {
            done(500,err);
        }
        console.log('deserializing user:',user.username);
        done(err, user);
    });
});

passport.use('login', new localStrategy({
    passReqToCallback : true
},
    function(req, username, password, done) {

        User.findOne({'username': username},

            function(err, user) {

                if(err) {
                    return done(err);
                }            
                if(!user) {
                    console.log("UserName or Password Incorrect");
                    return done(null, false);
                }
                if(!isValidPassword(user, password)) {
                    console.log("UserName or Password is Incorrect");
                    return done(null, false);
                }

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


passport.use('signup', new localStrategy({
    passReqToCallback : true

}, function(req, username, password, done) {

        User.findOne({'username': username},

            function(err, user) {

                if(err) {
                    console.log("Error in signup");
                    return done(err);
                }
                if(user) {
                    console.log("Username already exist" + username);
                    return(null, false);
                }
                else {

                    var newUser = new User();

                    newUser.username = username;
                    newUser.password = createHash(password);

                    newUser.save(function(err) {
                        if(err) {
                            console.log("Error in saving user");
                            throw err;
                        }
                        console.log(newUser.username + ' Registration succesful');    
                        return done(null, newUser);
                    });
                }

            });
}));


var isValidPassword = function(user, password) {
    return bcrypt.compareSync(password, user.password);
}

var createHash = function(password) {
    return bcrypt.hashSync(password, bcrypt.genSaltSync(10), null);
}
};

Auth.js

var express = require('express');
var router = express.Router();

module.exports = function(passport) {

router.get('/success', function(req, res) {

    res.send({state: 'success', user: req.user ? req.user : null});
});

router.get('/failure', function(req, res) {

    res.send({state: 'failure', user: null, message: 'Invalid Username or Password'});
});

router.post('/login', passport.authenticate('login', {

    successRedirect: '/auth/success',
    failureRedirect: '/auth/failure'
}));

router.post('/signup', passport.authenticate('signup', {

    successRedirect: '/auth/success',
    failureRedirect: '/auth/failure'
}));

router.get('/logout', function(req, res) {

    req.logout();
    res.redirect('/');
});

return router;

};

Server.js

var express = require('express');
var path = require('path');
var app = express();
var server = require('http').Server(app);
var logger = require('morgan');
var passport = require('passport');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var mongoose = require('mongoose');
var MongoStore = require('connect-mongo')(session);

 mongoose.connect("mongodb://localhost:27017/scriptknackData");
 require('./models/model');

 var api = require('./routes/api');
 var auth = require('./routes/auth')(passport);

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(passport.initialize());
app.use(passport.session());

app.use(session({
secret: 'super secret key',
resave: true,
cookie: { maxAge: 60000 },
saveUninitialized: true,
store: new MongoStore({ mongooseConnection: mongoose.connection })
}));

var initpassport = require('./passport-init');
initpassport(passport);

app.use('/api', api);
app.use('/auth', auth);

 // catch 404 and forward to error handler
 app.use(function(req, res, next) {
 var err = new Error('Not Found');
 err.status = 404;
 next(err);
});


var port = process.env.PORT || 3000;
server.listen(port, function() {

console.log("connected");
});
like image 258
aswheels media Avatar asked Aug 26 '17 08:08

aswheels media


People also ask

Does session expire on refresh?

refreshing a web page will not expire or create a new session in server. session is a user(client) based variable will create and store when the user first request send to the web server. so when you refresh a currently rendered page, only expiration time will reset.

Is express-session stateless?

ExpressJS Online Training. HTTP is stateless; in order to associate a request to any other request, you need a way to store user data between HTTP requests. Cookies and URL parameters are both suitable ways to transport data between the client and the server. But they are both readable and on the client side.

How does session work in express?

Overview. Express. js uses a cookie to store a session id (with an encryption signature) in the user's browser and then, on subsequent requests, uses the value of that cookie to retrieve session information stored on the server.

Where is express-session data stored?

Session data is stored server-side. Note Since version 1.5. 0, the cookie-parser middleware no longer needs to be used for this module to work. This module now directly reads and writes cookies on req / res .


1 Answers

As per express-session documentation

cookie.maxAge Specifies the number (in milliseconds) to use when calculating the Expires Set-Cookie attribute. This is done by taking the current server time and adding maxAge milliseconds to the value to calculate an Expires datetime. By default, no maximum age is set.

And use express.session() before passport.session() to ensure login session is stored in correct order. passport docs

In your case you have specified maxAge as 60000ms (60sec) only. Try this:

...
app.use(session({
secret: 'super secret key',
resave: true,
cookie: { maxAge: 8*60*60*1000 },  // 8 hours
saveUninitialized: true,
store: new MongoStore({ mongooseConnection: mongoose.connection })
}));

app.use(passport.initialize());
app.use(passport.session());
...

Increase your cookie maxAge value according to your need, it will solve your issue.

like image 96
shezy Avatar answered Oct 30 '22 15:10

shezy