So I made a MERN app and everything worked fine locally, so I decided to deploy it using heroku. I created to apps, one for client and one for server and I created my db in mongoDb Atlas and then connected it to my server. The problem is that when I try using the app on Chrome I can create a user en login but nothing after that. I don't see any cookie appearing and if I log what happens in Heroku I see that my isAuthenticated() function doesn't work (cannot read id of undefined). Then I tried on firefox and everything is working perfectly on this browser. Also it doesn't work on any other browser that I tried, like opera won't even let me register or login a user. I think it has something to do with the session and cookies but I don't see what and I've been scratching my head for days now... Here is my code in Express for the user Session and Mongo connection :
app.use(
cors({
origin: process.env.FRONTEND_URL,
credentials: true,
})
)
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
app.use(
session({
store: new MongoStore({ mongooseConnection: mongoose.connection }),
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
cookie: {secure: true, maxAge: 10000},
})
);
// for connecting mongodb Atlas
const MongoClient = require('mongodb').MongoClient;
const uri = process.env.MONGODB_URI;
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true
});
client.connect(err => {
const collection = client.db("test").collection("devices");
// perform actions on the collection object
client.close();
});
And in the client for my axios requests :
const service = axios.create({
baseURL: process.env.REACT_APP_BACKEND_URL,
withCredentials: true, // Cookie is sent to client when using this service. (used for session)
});