I built a simple user authentication app with React, node and Passport JS. When on localhost, it successfully stores the session and persist the user but after deploying on heroku it doesn't persist the session.
I checked networks tab and found that sameSite attribute is by default set to lax
and if client wants to get cross site cookies it should be set to none and must have secure attribute set. But when I set sameSite to none and secure to true, client don't get the cookie from the server (e.g set-cookie header is not present in response)
.
I don't know where am I doing wrong.
This is my Server JS
// Middlewares
app.use(express.json());
app.use(
cors({
origin: "https://authclient.netlify.app",
credentials: true,
})
);
app.use(
session({
secret: "secret",
resave: true,
saveUninitialized: true,
cookie: {
sameSite: "none",
secure: true,
},
store: MongoStore.create({ mongoUrl: dbURI }),
})
);
app.use(cookieParser("secret"));
app.use(passport.initialize());
app.use(passport.session());
require("./passportConfig")(passport);
//______________________________________
//Routes
app.post("/login", (req, res, next) => {
passport.authenticate("local", (err, user, info) => {
if (err) throw err;
if (!user) res.send(info.message);
else {
req.logIn(user, (err) => {
if (err) throw err;
res.send("Logged In!");
});
}
})(req, res, next);
});