connect.sid not getting stored in cookie

Viewed 259

I have a node express application.


const session = require('express-session');
const config = require('config');
var MemoryStore = require('memorystore')(session);


module.exports = function (app) {
    app.use(express.json());
    app.use(
        session({
            saveUninitialized: false,
            cookie: { maxAge: 86400000 },
            store: new MemoryStore({
                checkPeriod: 86400000
            }),
            resave: false,
            secret: config.get('Storagehash')
        })
    );
    app.use('/api/auth', users);
}


I have separated auth route and put it in a separate file like this. When I do console.log(req.session) I'm getting proper output.


const router = express.Router();


router.post('/', async (req, res) => {

....
    req.session.isAuth = true;    
    console.log(req.session);
    req.session.customerID = customer;
 
    res.send(token);


}

But when I'm looking in the cookie tab, connect.sid is not getting inserted there.

1 Answers

Do you have a frontend application? If so, whenever you send a request to your backend you need to include withCredentials: true in your request. This will send the cookies to your backend. If you are using axios to make requests it can be done like this:

(Assuming your port is 5000) axios.post("http://localhost:5000/api/auth/", {}, { withCredentials: true });

Related