我在港口运行一个3 000个当地客座反应堆和一个4000个快车服务器。 当我提出邮局的请求时,这些厨师正在组建之中,在把厨师附属于头部之后提出的任何要求也正在成立。 但是,在React中,我正在接受复函,但在我发出另一项请求时,收到的 co子并不附属于头部。 请帮助我如何做到这一点。
我的服务器正在利用这一功能,把 co子放在对策上:
const jwt = require( jsonwebtoken );
exports.generateToken = (res, userId) => {
const token = jwt.sign({ userId }, process.env.JWT_SECRET, {
expiresIn: 5d ,
});
res.cookie( jwt , token, {
path: / ,
httpOnly: false,
secure: false,
sameSite: None ,
maxAge: 5 * 24 * 60 * 60 * 1000,
});
};
这一功能被用于认证 co:
exports.protect = async (req, res, next) => {
let token;
try {
token = req.cookies.jwt;
} catch (error) {
sendError(res, "Invalid Token Provided. Please Login again to continue", 401);
createError(error, "middleware/authMiddleware.js/protect");
return; // Stop execution here after sending the error response
}
const role = req.body.role || req.params.role;
if (!token) {
return sendError(res, "Invalid:token Please Login to Continue", 401);
}
try {
const decoded = jwt.verify(token, process.env.JWT_SECRET);
if (!isValidObjectId(decoded.userId)) {
return sendError(res, "Unautherised Access", 401);
}
const user = await Users.findOne({_id:decoded.userId});
if(!user) return sendError(res,"Invalid User Id Found",404);
if (role !== user.role) {
return sendError(res, "You are not authorized to access this route", 401);
}
if (user._id!=decoded.userId) {
return sendError(res, "User Not Found", 404);
}
req.user = {
_id: decoded.userId,
name: user.name,
email: user.email,
role: user.role,
department: user.department,
};
next();
} catch (error) {
sendError(res, "Invalid Token Provided. Please Login again to continueee", 401);
createError(error, "middleware/authMiddleware.js/protect");
}
};
我正在办理向浏览器发送 co子的信号路线,但并未确定:
但是,在申请表上,没有发现 co子:
我在发出这一请求时使用的是:
const handleVerifyUser = async () => {
setName(() => "");
setPassword(() => "");
setDepartment(() => "");
setEmail(() => "");
const res = await fetch("http://localhost:4000/api/v2/verifyUser", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
credentials: "same-origin",
withCredentials: true,
body: JSON.stringify({
token: otp,
}),
});
const data = await res.json();
console.log(data);
if (data.success === true) {
setMessage(() => data.message);
setTimeout(() => {
setMessage(null);
}, 2000);
console.log(data);
} else {
setMessage(() => data.message);
setTimeout(() => {
setMessage(null);
}, 2000);
}
};
我正在得到答复。
{电文:“因果: 请登录继续”}
在发出请求后,如果我看到网络把收到的厨师列为下一份申请的负责人,他就没有发出这一请求。
I tried multiple times from different browsers but the cookie was not being set anywhere