This may be because for cross-domain cookies you set {sameSite: true} and {secure: true}, but in your example you are doing it on http://localhost so it won't Set any cookies. Please refer to the link below for requirements.
Also set the correct headers, such as Access-Control-Allow-Credentials, Access-Control-Allow-Origin, Access-Control-Allow-Headers
You can use mkcert to refer to making a secure connection on localhost.
I also recommend using the same top-level domain on both frontend and backend, and using subdomains.
Another thing to note here is that if there is a port in the domain name, I don't think Chrome will set the cookie, please give it a try.
I successfully solved this problem so that others who come later can find the answer. I moved the declaration of cookieparser to just before where the sequelize connection is initialized. I also added the withCredentials option to my axios post request. With both steps, my cookies are now set correctly and accessible.
const express = require("express"); require("dotenv").config(); const cors = require("cors"); const app = express(); app.use(express.json()); app.use(express.urlencoded({ extended: true })); const cookieParser = require("cookie-parser"); app.use(cookieParser()); const port = process.env.PORT || 8080; const lib = require("./lib"); //這是所有自定義函數(shù) const sql = require("./database");
onSubmit() { let loginInfo = { email: email.value, password: password.value, }; axios .post("http://localhost:3000/user/login", loginInfo, { withCredentials: true, }) .then(() => $q.notify({ color: "green-4", textColor: "white", icon: "cloud_done", message: "帳戶創(chuàng)建成功!", }) ) .catch(() => $q.notify({ color: "red-5", textColor: "white", icon: "warning", message: "電子郵件或用戶名已被使用!", }) ); },