P粉5140018872023-08-30 16:22:05
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.
P粉6474494442023-08-30 09:52:10
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"); //这是所有自定义函数 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: "帐户创建成功!", }) ) .catch(() => $q.notify({ color: "red-5", textColor: "white", icon: "warning", message: "电子邮件或用户名已被使用!", }) ); },