I have an issue with not being able to access a custom variable I set to the session object when a user logs into my service.
Post request from front-end
const sendChat = async (contents, cid, agentID) => {
const config = {
headers: {
"Content-Type": "application/json",
},
withCredentials: true,
};
const body = JSON.stringify({
channelID: cid,
message: contents,
agentID: agentID,
});
console.log(`AgentID: ${agentID}`);
return await axios.post(
"http://localhost:5000/api/chat/sendMessage/",
body,
config
);
};
Session configuration in server
app.use(
session({
secret: config.get("sessionSecret"),
name: "sid",
cookie: { maxAge: 990000, httpOnly: false },
resave: true,
store: MongoStore.create({ mongoUrl: config.get("mongoURI") }),
saveUninitialized: false,
})
);
In both cases when I login to the service, through POSTMAN or with axios in the front-end, it creates a session containing the userID field that I can see when I check my store (mongoDB). However, when I get a request from axios, req.session.userID is undefined when try to access it in a route -- whereas with postman, it is defined when I try to access it in the route. I have cors enabled in the server.
const corsOptions = {
origin: "http://localhost:3000",
credentials: true,
optionSuccessStatus: 200,
};
app.use(cors(corsOptions));
Why does it work with POSTMAN but not through the front-end localhost?
EDIT: I made a logical error in my front-end, I had added the withCredentials: true property to the wrong request.