0

I'm using the express-session package and I want to change the variable "_id" in the session. Here my session init

app.use(session({
    secret: "secretshhhhhh",
    resave: true,
    saveUninitialized: false,
}))

After the login page I try to store the id with these few lines:

req.session._id = user._id.toString()
req.session.save(function (err) {
    req.session.save(function (err) {
         console.log(req.session)
    }) 
})

The console.log print the session with the id, but when I try to get the _id in an other page express send me back a null object. Here an exemple of printing session without the _id

return res.status(200).send(req.session); 

I tried many methods but none of these worked for me.

EDIT: Here my whole function to put it in session

module.exports.login_post = async function (req, res) {
    User.findOne({ email: req.body.email }, function (err, user) {
        if (user == null) {
            return res.status(400).send({
                message: "User not found"
            })
        }
        else {
            if (user.validPassword(req.body.password)) {
                req.session._id = user._id.toString()
                req.session.save(function (saveErr) {
                    req.session.reload(function (reloadSave) {
                        console.log(req.session, saveErr, reloadSave)
                    }) 
                })
            }
        }
    })
}

Here my whole function to get it from session

module.exports.session_get = function(req, res) {
    return res.status(200).send(req.session); 
}

module.exports.session_destroy = function(req, res) {
    req.session.destroy();
    return res.status(200).send({
        message: "Session detroyed"
    }); 
}
Gregoirelpv
  • 92
  • 1
  • 10
  • Do you use a browser or another client to navigate the site? – IAmDranged Apr 23 '21 at 11:22
  • @IAmDranged Yeah i'm using a HashRouter from @types/react-router-dom for my front. For my back I'm just using express routes. – Gregoirelpv Apr 23 '21 at 11:23
  • does `console.log(err, req.session)` print anything interesting? `_id` is a special field used by mongodb as a unique identifier for the document. – Alex Blex Apr 23 '21 at 11:25
  • nothing just the session with the _id like I want, that's when I try to print the session with the API that the _id disappeared – Gregoirelpv Apr 23 '21 at 11:29
  • Are your front-end and back-end apps being served from different origins? – IAmDranged Apr 23 '21 at 11:38
  • No because I build the front so the back can use it. They are all on the same server on the same port – Gregoirelpv Apr 23 '21 at 11:44
  • Ok, what library do you use on your front-end to query the server? If you use fetch for instance, you have to explicitly set it up to send cookies. https://stackoverflow.com/questions/34558264/fetch-api-with-cookie – IAmDranged Apr 23 '21 at 11:47
  • No because I fetch the session from the back. – Gregoirelpv Apr 23 '21 at 11:49
  • Could you provide more of your code - the whole login controller for instance? – IAmDranged Apr 23 '21 at 12:13
  • 1
    So you never actually send a response to the client after setting _id on the session. So the session cookie is never actually received by the browser - and thus never sent on subsequent requests to the server either. – IAmDranged Apr 23 '21 at 16:25

0 Answers0