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

82
Views
Nodejs app suddenly started to act differently

This part of code handles the login authorization routing in my app

const express = require("express");
const authController = require('../controllers/authController');
const indexController = require('../controllers/indexController');
const router = express.Router();

router.use("/login", (req, res, next) => {
  if(req.session.loggedIn) {
    res.redirect('/account');
  }
  next();
});

router.use("/account", (req, res, next) => {
  if(!req.session.loggedIn) {
    res.redirect('/login');
  }
  next();
});

router.get('/', (req, res) => {
  res.redirect('/login');
});
router.get('/login', (req, res) => {
  res.render('login');
});
router.get('/log-out', authController.logOut);
router.get("/account", indexController.getAccountData);

module.exports = router;

There were no problems and it was working fine till recent days.

I haven't change anything in this file nor authController nor indexController. When I make a change (in other parts), nodemon restarts the app and I automatically jump to login page cause obviously all sessions are destroyed. But I get an error in getAccountData function (Error says req.session is undefined).

As you can see there's no way for the app to reach that function with no sessions set.

I have to restart the app again to act correct.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

req.session.loggedIn would error out whenever session is undefined. You need to check if it is defined before trying to access loggedIn.

Try:

router.use("/login", (req, res, next) => {
  if(req.session && req.session.loggedIn) {
    res.redirect('/account');
  }
  next();
});

router.use("/account", (req, res, next) => {
  if(!req.session || !req.session.loggedIn) {
    res.redirect('/login');
  }
  next();
});
about 4 years ago · Juan Pablo Isaza Report

0

The session will be cleared each time the server restarts. So to escape from that you need to save the session to database. If you are using mongodb or I can give you example using mongodb.

import MongoStore from "connect-mongo";
import session from "express-session";

app.use(
  session({
    store: MongoStore.create({
      mongoUrl: process.env.MONGODB_URI || "mongodb://localhost:27017/project",
    }),
    secret: "secret key",
    cookie: { maxAge: sessionExpireInMilliseconds },
  })
);
about 4 years ago · Juan Pablo Isaza 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!