我正在尝试使用 Hapi.js 在我的浏览器中设置一个 cookie。我遵循 hapi.js 文档,并确保将 isSecure 选项设置为 false,因为我将 http 连接用于开发目的。但是,我不知道为什么我的 cookie 没有出现在 chrome 开发工具的 cookie 选项卡下。但是,我确实在网络选项卡下的响应标题中看到了它。这是我的代码:
服务器.js
//Set Cookie configuration
server.state("loginCookie", {
ttl: null,
isSecure: false, //Must set to false for now. It will not work if true on a http connection
isHttpOnly: true,
encoding: "base64json",
clearInvalid: true, //Want to remove invalid cookies
strictHeader: true,
});
user_routes.js
{
method: "POST",
path: "/signup",
options: {
validate: {
payload: Joi.object({
username: Joi.string().alphanum().min(4).max(16).required(),
password: Joi.string().min(4).required(),
}),
},
pre: [{ method: verifyUniqueUser }],
auth: false,
},
handler: SignUpHandler,
},
注册.js
export const SignUpHandler = async (req, h) => {
try {
let user = new UserModel(req.payload);
//Hash Password first before storing in user Model
const { password, username } = req.payload;
const hash = await bcrypt.hash(password, 8);
user.password = hash;
await user.save();
let token = jwt.sign(
{ id: user._id, username: username },
process.env.SECRET_KEY,
{ algorithm: "HS256", expiresIn: "1h" }
);
console.log(token);
h.state("loginCookie", token);
return "Signed Up";
} catch (error) {
throw Boom.badRequest(error);
}
};
我的前端部分向我的 hapi 服务器发出请求
const makeAccount = async () => {
console.log(credentials);
await axios.post("http://localhost:3001/signup", credentials);
};
知道我可能做错了什么吗?