I am working on a React / NodeJS (+ Postgres) project.
I have a basic login page (on react) and an endpoint (on node) that assert my user is on database. If yes I set in the user in the session :
app.post('/login', (req, res) => {
authentication.verifyLogin(req.body.username, req.body.password, (result) => {
if (result.status === "KO") {
res.sendStatus(401).send(result.data)
return
}
if (result.status === "OK") {
req.session.user = result.data
res.send(result.data)
}
});
});
- I setup everything in the back to work fine :
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', `${process.env.CLIENT_ENDPOINT}`);
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
app.use(
session({
secret: 'MYCODE',
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: false,
sameSite: "none",
secure: true,
maxAge: 24 * 60 * 60000
}, // value of maxAge is defined in milliseconds.
})
);
- Also in the front
fetch(`${BASE_URL}/${endPoint}`, {
headers: { 'Content-Type': 'application/json' },
"method": HTTPMethod.POST,
"credentials": 'include',
"body": JSON.stringify(body),
mode: 'cors'
})
- My serveur : the nodejs server runs in a docker, and I use Lightsay (AWS) on a domain, for the React I use Amplify (AWS), on a other domaine, both have https (SSL) certificate.
Everything works fine on the localhost, I can log-in, I do have the session that keeps my user, but when I push my code to AWS, the session is not persisted : the req.session is empty. But from other call that does not need the session I can access everything.
I tried using coockie-session on the nodejs, I try multiple configs on the “app.use(session({})” part, using req.session.save() but everytime it failed. I know it’s some basic stuff to have a front and a back but I could not make it works properly. I have look for tutorial from internet, asked chatgpt and everything, but nothing works.
Did I do something wrong ? Is it some basic knowledge that I missed ? Does the fact that I use docker is an issue in my case ?
If there are some basics projects / templates or git repo that works fine I will take it.
Thanks for the help !
ThePaulohubert is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.