I have an app (localhost:3000) and a REST Api (localhost:3001). Each user makes a call to the API with an unique token. I am trying to save this token on the server side through sessions, as follows:
app.get("/endpointA", (req, res, next) => {
req.session.token = req.query.token;
req.session.save(next);
}, …);
After several redirects to 3-rd party services, it redirects users back to my API, but the old session is not available at this point (session id has changed):
app.get("/endpointB", (req, res, next) => {
console.log(req.session.id);
console.log(req.session.token);
});
I've tried to regenerate session, but it outputs Error: failed to load session. Any ideas of why the req.session.id is changing after those redirects?
=> localhost:3001/endpointA
Session id: 1FR9LZOWnhWlvE7grIUuXQmbzUSvw08V
Token: 57wGg0AW_-EG0-EyAAAB
=> localhost:3001/endpointB
Session id: ezIHVpwFHHipc8fQeGrSbRVhFmedtczF
Token: undefined
There are a lot of possibilities and nothing specific jumps out from the description. Is it possible you can provide all the following so we can take a look?
I've found the issue, it's quite embarrassing. I thought that 127.0.0.1:8000 and localhost:8000 are the same, but apparently those are handled differently by express.js. So the client was requesting 127.0.0.1:8000 and 3-rd party service was returning data to localhost:8000 - thus the session ID was different for each _domain_.
Most helpful comment
I've found the issue, it's quite embarrassing. I thought that
127.0.0.1:8000andlocalhost:8000are the same, but apparently those are handled differently byexpress.js. So the client was requesting127.0.0.1:8000and 3-rd party service was returning data tolocalhost:8000- thus the session ID was different for each _domain_.