cookie is not being set on production NodeJS express app and react app

Viewed 39

Although everything was working in the local development environment, when I deployed it to production, the cookies were not being set.

i hosted the backend in heroku and the frontend in netlify and everything is working the only thing that is not working is the cookies

const express = require("express");
const app = express();
const cookieParser = require("cookie-parser");
const authRoute = require("./routes/auth-route.js");

/* A middleware that allows cross-origin requests. */
app.use(
  cors({
    credentials: true,
    origin: "https://myfrontend.com",
  })
);

/* Parsing the cookies in the request object. */
app.use(cookieParser({}));

/* A middleware that parses the request body and makes it available in the request object. */
app.use(express.json());

//api routes
/* A middleware that is used to route the requests to the authRoute.js file. */
app.use("/api/auth", authRoute);

my auth-route.js

const router = require("express").Router();
const {login} = require("../controllers/auth-controller");
router.post("/login", login);
module.exports = router;

my auth-controller.js

const bcrypt = require("bcrypt");
const User = require("../models/User");
const generateJwt = require("../utils/generateJWT");
const login = async (req, res) => {
    try {
      /* Finding a user in the database with the email that was sent in the request body. */
      const user = await User.findOne({ email: req.body.email });
      /* This is checking if the user exists in the database. If the user does not exist, it will return
            a 404 status code and a message. */
      if (!user)
        return res.status(404).json({ success: false, message: "Email or Password Incorrect" });
    
      /* Comparing the password that was sent in the request body to the password that is stored in the
        database. */
      const validatePassword = await bcrypt.compare(req.body.password, user.password);
  
      /* This is checking if the password that was sent in the request body matches the password that is
         stored in the database. If the password does not match, it will return a 404 status code and a
         message. */
      if (!validatePassword)
        return res.status(404).json({ success: false, message: "Email or Password Incorrect" });
  
      /* Destructuring the user object and removing the password from the object. */
      const { password, ...others } = user._doc;
      /* Generating a JWT token. */
      const token = generateJwt(user._id);
  
      /* Setting a cookie on the client side. */
      //if rememberMe is checked in the client the cookie will last for 30 day if not only for session
      if (req.body.rememberMe === true) {
        res.cookie("isLogged", true, {
          sameSite: "none",
          secure: true,
          domain: "myFrontend.com",
          maxAge: 2592000000, //30 day = 2,592,000,000 msg
        });
  
        res.cookie("authToken", token, {
          httpOnly: true,
          sameSite: "none",
          domain: "myFrontend.com",
          secure: true,
          maxAge: 2592000000, //30 day = 2,592,000,000 msg
        });
      } else {
        res.cookie("isLogged", true, {
          sameSite: "none",
          domain: "myFrontend.com",
          secure: true,
        });
  
        res.cookie("authToken", token, {
          httpOnly: true,
          sameSite: "none",
          domain: "myFrontend.com",
          secure: true,
        });
      }
      res.status(200).json({ success: true, user: others });
    } catch (err) {
      console.log(err);
      res.status(500).json({ success: false, message: err });
    }
  };

  module.exports = {login };
0 Answers
Related