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

86
Views
express session disappears as soon a I reload front end

I am using express sessions and cookies to keep logged in a session, but it is not working, as soon as I reload the react app to check if the session works, all I get is the req.session.user was deleted (undefined), here's my express code below:

app.use(session({
  key: "userId",
  secret: "test",
  resave: false,
  saveUninitialized: false,
  cookie: {
    expires: 60 * 60 * 24 * 1000
  }
}))

app.get("/login", async (req, res) => {
  console.log(req.session.user)
  if (req.session.user) {

    res.send({ loggedIn: true, user: req.session.user })
  }
  else {
    res.send({ loggedIn: false });
  }
})
app.post("/login", async (req, res) => {
  try {
    if (req.body) {
      const { formData } = req.body;


      const sqlfetch = "Select * FROM users WHERE username=?"
      await db.query(sqlfetch, formData.username, async (err, result) => {
        if (err) {
          console.log(err);
          res.sendStatus(500);
        }

        else if (result.length > 0) {
          await bcrypt.compare(formData.pass, result[0].pass, (error, response) => {
            console.log(response)
            if (response) {
              req.session.user = result
              res.send({ loggedIn: true })
            }

            else {
              return res.send({ loggedIn: false }).status(401);

            }
          });
          
        }

      });

    }
  } catch (error) {
    console.log(error)
  }

})

but all I get is loggedIn:false any help please?? Thank you

about 4 years ago · Juan Pablo Isaza
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!