gpt4 book ai didi

res.cookie is not working but Postman shows Cookies(Res.Cookie不工作,但邮递员显示Cookie)

转载 作者:bug小助手 更新时间:2023-10-25 19:36:53 24 4
gpt4 key购买 nike



I'm having issues with res.cookies not setting cookies from the backend to the frontend on the browser. Additionally, [req.cookies] isn't reading cookies from the frontend browser, even if I hardcode the cookies from the frontend. I've tried various solutions, including setting withCredentials: true, but it's not sending or reading the cookies as expected. Here's my code:

我有问题的res.cookies没有设置从浏览器的后端到前端的cookie。此外,[req.cookies]不能从前端浏览器读取cookie,即使我从前端硬编码cookie。我尝试了各种解决方案,包括使用Credentials:True进行设置,但它没有按预期发送或读取Cookie。以下是我的代码:


// 1.)
import { createAsyncThunk } from "@reduxjs/toolkit";
import axios from "axios";
import { login_success, login_fail, login_request, clear_errors, signup_request, signup_fail, signup_success, load_user_request, load_user_fail, load_user_success } from "../reducers/userReduces.js";
import { withCookies } from "react-cookie";
// axios.defaults.withCredentials = true;

export const login = createAsyncThunk("login", async (reqbody, thunkAPI) => {
try {
const { email, password } = reqbody;
thunkAPI.dispatch(login_request());
const config = {
headers: { "Content-Type": "application/json" },
withCredentials: true,
};

const { data } = await axios.post("http://localhost:4000/ecom/v1/login", {
email,
password,
config,
});

console.log(data);
thunkAPI.dispatch(login_success(data));
} catch (error) {
console.log(error);
thunkAPI.dispatch(login_fail(error));
}
});

// 2.)
router.route("/login").post(logUser);

exports.logUser = catchAsyncErrors(
async (req, res, next) => {
const { email, password } = req.body;

if (!email || !password) {
return next(new ErrorHandler("Enter email & password", 400));
}

const user = await User.findOne({ email }).select("+password");

if (!user) {
return next(new ErrorHandler("Invalid email or password"));
}

const ispassword = await user.comparePassword(password);

if (!ispassword) {
return next(new ErrorHandler("Invalid email or password"));
}

sendToken(user, 200, res);
console.log("Sent newcookie request");
}
);

// 3.)
const sendToken = async (user, statusCode, res) => {
const token = await user.jwtToken();

const options = {
expiresIn: new Date(
Date.now() + (process.env.COOKIE_EXPIRES_IN * 24 * 60 * 60 * 1000)
),
httpOnly: true,
secure: true,
sameSite: "None",
};

console.log("Sending token to browser");
res.cookie("token", token, options);
console.log("Sent cookie");

res.status(statusCode).json({
success: true,
user,
token,
});
};

module.exports = sendToken;

// 4.)
app.use(cookieParser());
app.use(cors({ origin: "http://localhost:3000", credentials: true }));
app.use(express.json());


I did withCredentiales:true and i added origin in cors() and also
httpOnly: true,
secure: true,
sameSite: "None",
but it wont work

更多回答
优秀答案推荐
更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com