Я работаю над фронтом React и Express Backend, и я пытаюсь отправить запрос входа в систему с помощью Axios. Тем не менее, необходимое не включает в себя файлы cookie, даже если я использую Credentials: True. и Cros, и есть токен в печенье, я получил неопределенную < /p>
import axios from "axios";
const login = async () => {
try {
const response = await axios.post(
"http://localhost:7000/api/auth/login",
{
email: "test@example.com",
password: "password123",
},
{
withCredentials: true, // Ensures cookies are sent and received
headers: {
"Content-Type": "application/json",
},
}
);
console.log(response.data);
} catch (error) {
console.error("Login error:", error.response?.data || error.message);
}
};
login();
< /code>
import "dotenv/config";
import express from "express";
import cors from "cors";
import cookieParser from "cookie-parser";
import connectDb from "./config/connectDb.js";
import authRoute from "./routes/authRoute.js";
const app = express();
const port = process.env.PORT || 4000;
connectDb();
app.use(express.json());
app.use(cookieParser());
app.use(
cors({
origin: "http://localhost:5173",
credentials: true
})
);
app.use("/api/auth", authRoute);
app.listen(port, () => console.log(`Server is running on port ${port}`));
< /code>
What I've Tried:
Used withCredentials: true in axios request.
Enabled credentials: true in cors middleware.
Used cookieParser() in Express.
Verified that Set-Cookie is included in the response headers (checked via Postman & Network tab in Chrome DevTools).
Checked browser settings to ensure third-party cookies are not blocked.
Подробнее здесь: https://stackoverflow.com/questions/794 ... om-backend