res sessions not saving

31 views Asked by At

Currently I have my applications hosted and am now tryingto get my request sessions to work. Once I hosted my websites I realized that after logging in with google oauth, a user was made in my database, however when assigning req.session.user = req.user, it no longer saves and I dont understand why. I have currently changed some things that gave me errors like making my sessions go tomy database instead of memeory because of memory leek errors but that did not resolve my issue. Part of the problem could be my lack of knowledge and I dont fully understand how the mysql express storage is supposed to be utilized however it hasnt solved my problem after setting it up.

How it works When a user logs in with google they reroute to the success endpoint here

router.get("/google/success", (req, res) => {
  req.session.user = req.user;

  console.log({log: "success", user: req.session.user})
  res.redirect(`${process.env.FRONTEND_URL}/landing`);
});

After this the landing page refreshed and an api is called everytime yougo to the landing page that checks if someone is logged in and reroutes to this.

router.get("/login", LoggedIn, (req, res) => {
  res.status(200).json({result: req.session.user, status: 200});
});

Now the issue comes in the LoggedIn middleware.

export function LoggedIn(req, res, next) {
  console.log({log: "LoggedIn Middleware ", session: req.session});
  if (req.session.user) {
    next();
  } else {
    res.send({ error: "unauthorized", status: 401 });
  }
}

For some reason user is not being saved as a property on the session.

In the console logs when hitting the succes routes it even shows the session containing the user enter image description here

but when hitting the middleware and console logging it its not in there. enter image description here

0

There are 0 answers