Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

288
Views
How to keep user logged in even after reloading the main file (index.js)?

I have an Express app that uses express-session and passport to keep the session going.

These technologies keep users logged in until they hit a logout button.

But, there is one exception that makes users log out.

I use PM2 to reload my index.js file. The command is below.

pm2 reload all

When I execute the above command, index.js file gets reloaded and everything looks fine.

But, I realized that every time I reload index.js file using this command, logged in users get logged out, which should not happen.

Can anyone tell me how to keep users logged in even after I reload the index.js file?

Some of the code in index.js file:

var express = require("express");
var app = express();
var cookieParser = require("cookie-parser");
var bodyParser = require("body-parser");
var session = require("express-session");

passport.serializeUser(function(user, done) {
  done(null, user.email);
});

passport.deserializeUser(function(username, done) {
  findByUsernameDb(username, function(err, user) {
    if (err) {
      return done(err);
    }
    done(err, user);
  });
});

passport.use("login", new LocalStrategy({
    usernameField: "email",
    passwordField: "password"
  },
  function(username, password, cb) {
    findByUsernameDb(username, function(err, user) {
      if (err) {
        return cb(err);
      }
      if (!user) {
        return cb(null, false, { message: "Incorrect email." });
      }
      bcrypt.compare(password, user.password, function(err, res) {
        if (res) {
          return cb(null, user);
        } else {
          return cb(null, false, { message: "Incorrect password." });
        }
      });
    });
  }
));



app.use(cookieParser('hello'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(session({ 
  secret: 'world',
  resave: true,
  saveUninitialized: true
}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Use one of persistent session storage engine. Here is a list https://github.com/expressjs/session#compatible-session-stores

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!