Can’t find a cookie for my domain to use for authentication

I am learning how to do a JWT authentication process with React and NodeJS( Express). I used postman to execute the following function

const express = require('express');
const router = express.Router();
const jwt = require('jsonwebtoken');

// Secret key used to sign the tokens
const secretKey = '/Phc{m=MG$nv)UNb&j8k?djaRD)-FQ/3QK,aYT}9a/):FmSk2H';

// Verify token function
function verifyToken(token) {
  try {
    const decoded = jwt.verify(token, secretKey);
    return decoded; // Token is valid
  } catch (error) {
    return null; // Token is invalid
  }
}

// Route for token verification
router.post('/verifyToken', (req, res) => {
  const { accessToken } = req.body;
  if (!accessToken) {
    return res.status(400).json({ error: 'Access token is required' });
  }
  const decodedToken = verifyToken(accessToken);
  if (decodedToken) {
    // Token is valid, send success response
    return res.json({ isValid: true });
  } else {
    // Token is invalid, send error response
    return res.json({ isValid: false });
  }
});

// Export the router
module.exports = router;
We initialize the route in the entry file of our node app

const verifyTokenRouter = require('./routes/VerifyToken')
app.use("/", verifyTokenRouter)


// Import necessary modules
const express = require("express");
const app = express();
const cors = require("cors");
const db = require('./models');

// Middleware
app.use(express.json());

app.use(cors({
    origin: 'http://localhost:3000', // Allow requests only from http://localhost:3000
    credentials: true // Allow cookies to be sent with the request
  }));

With postman the request works just fine , but I have a function to automatically take our cookie and execute verifyToken via axios , however baffling enough it fails because it cannot find the cookie for in my document

useEffect(() => {
    // Function to retrieve the value of a cookie by name and domain
    function getCookie(name, domain) {
      const cookies = document.cookie.split(';');
      for (let i = 0; i < cookies.length; i++) {
        const cookie = cookies[i].trim();
        // Check if this cookie matches the desired name and domain
        if (cookie.startsWith(name + '=') && cookie.includes(`Domain=${domain}`)) {
          // Return the value of the cookie
          return cookie.substring(name.length + 1);
        }
      }
      // Return null if the cookie is not found
      return null;
    }

    // Retrieve the access token cookie from localhost domain
    const accessToken = getCookie('accessToken', 'localhost');

    // Example usage
    if (accessToken) {
      // Make a request to your server's API endpoint for token verification
      axios.post('http://localhost:3001/auth/verifyToken', { accessToken })
        .then(response => {
          const isValid = response.data.isValid; // Assuming the server returns the verification result
          console.log('Token verification result:', isValid);
          if (isValid) {
            console.log('Sign In successful');
          } else {
            console.log('Sign In failed');
          }
        })
        .catch(error => {
          console.error('Error verifying token:', error);
        });
    } else {
      console.error('Access token cookie not found');
    }
  }, []);

So just to clarify again only the last block of code fails it returns the else error of App.jsx:48 Access token cookie not found so getCookie accessToken fails for some reason ? Initially I thought it may not know the domain so I added this line if (cookie.startsWith(name + '=') && cookie.includes(Domain=${domain}`)) I also tried to remove the name of the cookie and identify by the domain name alone but i still have the same error.

I’m really not sure what I am doing wrong

I searched for similar issues but not one of them really match my case.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật