I deployed my React frontend to netlify, and express backend to Heroku, and I'm using session.
The session works perfectly locally to it doesn't work when I deploy it
When I check the db, session has been made, but when I console.log(req.session)
it doesn't logs anything
So, it looks like the session is saved on db, but it doesn't save on the heroku server
Do I need to configure sessions on heroku?
mongoose.connect(
"mydburl",
{
useNewUrlParser: true,
}
);
const store = new MongoDBStore({
uri:
"mydburl",
collection: "sessions",
});
app.use(
cors({
domain: url,
origin: [url],
methods: ["GET", "POST", "DELETE"],
credentials: true,
})
);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.set("trust proxy", 1);
app.use(
session({
key: "userID",
secret: "subscribe",
proxy: true,
resave: false,
saveUninitialized: false,
store: store,
cookie: {
secure: false,
httpOnly: false,
maxAge: null, //1000 * 60 * 60 * 24 * 7, 1 week
domain: url,
},
})
);
app.get("/login", (req, res) => {
if (req.session.user) {
console.log(req.session.user);
res.send({ isLogin: true, user: req.session.user });
} else {
console.log("req.session.user doesn't exist");
res.send("no");
}
});
app.post("/login", (req, res) => {
const { username, password } = req.body;
RegisterModel.find({ username }, (err, result) => {
if (err) {
console.log(err);
}
if (result.length > 0) {
bcrypt.compare(password, result[0].password, (error, response) => {
if (response) {
req.session.user = result;
res.send(result);
} else {
res.send("????? ?? ????");
}
});
} else {
res.send("???? ?? ????");
}
});
});
question from:
https://stackoverflow.com/questions/66062049/express-session-not-persisting-after-deploying-to-heroku-and-netlify