2

I am using cookie-session to check the id of an user:

const cookieSessionMiddleware = cookieSession({
    // secret: secretcookie.cookieSecret,
    secret: "dont look here, it will be a secret",
    maxAge: 100 * 60 * 60 * 24 * 14,
});

app.use(cookieSessionMiddleware);
app.post("/users", function (req, res) {
const name = req.body.name;
db.addUser(name)
    .then(({ rows }) => {
         const id = rows[0].id;
         req.session.userId = id;
        return rows[0];
    })
    .then((results) => {
        const name = results.name;
        res.json({ response: name, success: true });
        // console.log("rows-->", results);
    })
    .catch((e) => {
        res.json({ error: e, success: false });
    });
});

I am running a Create-react-app in localhost:3000 and the server.js in localhost:3001, being able to connect them with cors options.

I don't know why, but my app is not saving the cookie here. But if I do this, I can save a cookie:

app.get("/users", (req, res) => {
    req.session.userId = 5;
    console.log("from users", req.session);
    res.json({ response: req.session, success: true });
});

I checked the documentation, searched on google and looked here, but I didn't find solution. Someone knows why?

Thank you in advance!

Samball
  • 623
  • 7
  • 22
  • Does the line `req.session.userId = id;` inside `db.addUser(name).then...` run? Can you add a `console.log()` after that line to test it? – Simon Smith May 31 '22 at 10:06
  • Yes, I did a console log random to check if it reaches and it does after and before. but the req.session is empty there... – Gabriel Chamorro Moris May 31 '22 at 10:35
  • Is `rows[0].id` undefined? If so, `req.session.userId` will be cleared. – Simon Smith May 31 '22 at 11:13
  • It is not, I console log the id before and after the req.session.userId = id I can see the id after and before... I even wrapped it with TRY, catch, and logged into the try and it works, but no cookie is set... – Gabriel Chamorro Moris May 31 '22 at 11:14
  • How do you know whether a cookie is set successfully? Are you logging `req.session` outside `db.addUser`, but inside `function (req, res) {}`? If so, you should mark the function `async`, and add `await` before `db.addUser`. If you tested the cookie on the client side, and the server did behave as you stated, then I have no idea what is going on... – Simon Smith May 31 '22 at 12:01
  • It is very curuous, I did async await and I can now acces to the req.session INSIDE the function. But if i check in console app, in localhost:300 I have no cookie and if i want to check later to the cookie it is not there... – Gabriel Chamorro Moris May 31 '22 at 15:29
  • Maybe you are not using https when debugging. Did you pass `cookie: {httpOnly: true, secure: false}` to `app.use(session({...}))` ? Maybe you can use Postman to test your server. This tool behaves like a browser and saves cookies for you. – Simon Smith May 31 '22 at 16:34
  • I also tried... I dont have a clue... My server is running in localhost:3001 and my app in localhost3000, could it be the problem? – Gabriel Chamorro Moris May 31 '22 at 18:28
  • I was using Apollo server with `express.js` yesterday. It has a built-in CORS feature. I had to turn it off, so the requests can be handled by global CORS settings. You may check if any component of your server has CORS support itself. – Simon Smith Jun 01 '22 at 03:28

0 Answers0