bcrypt.compare 无法在 nextjs 中设置响应 headers
bcrypt.compare cannot set response headers in nextjs
当我的代码输入 bcrypt.compare 时,我似乎无法得到正确的响应 headers。一开始我以为是cors问题,但如果我输入错误并显示"user does not exist",我仍然得到正确的响应。
这是我在 expressapi 服务器端的代码
router.post("/api/signIn", (req, res) => {
const { user, password } = req.body;
const queryString = "SELECT * FROM users WHERE user_id = ?";
db.query(queryString, [user])
.then(result => {
if (result.length > 0) {
const hash = result[0].password;
//here bcrypt.compare works server side or with cURL but won't set the response headers in the browser
bcrypt
.compare(password, hash)
.then(same => {
if (same === true) {
console.log("correct password");
res.status(200).json({
code: 200,
message: "correct password"
});
} else {
res.status(401).json({
code: 401,
message: "incorrect password"
});
}
})
.catch(err => console.log(err));
} else {
//this one works though and i can get the response in the browser so it can't be a cors issue
console.log("user does not exist");
res.status(200).json({
code: 401,
message: "User does not exist"
});
}
})
.catch(err => {
console.log("error" + err.message);
});
});
这是我在 react 中使用的测试函数
const signIn = () => {
fetch("http://localhost:5000/api/signIn", {
method: "POST",
body: JSON.stringify({
user: userName,
password: password
}),
headers: {
"Content-Type": "application/json"
},
})
.then(res => res.json())
.then(response => alert(response.code + response.message))
.catch(err => alert(err));
};
所以如果我输入了不在数据库中的错误用户名,警报功能会显示(code401User 不存在)但是如果我输入正确的用户 bcrypt.compare() 似乎没有设置正确和错误密码的响应,我会得到(TypeError:无法获取)。不过在 cURL 中测试 api 是可行的。
知道了,我忘了把 event.preventDefault() 放在 fetch 函数上。
当我的代码输入 bcrypt.compare 时,我似乎无法得到正确的响应 headers。一开始我以为是cors问题,但如果我输入错误并显示"user does not exist",我仍然得到正确的响应。
这是我在 expressapi 服务器端的代码
router.post("/api/signIn", (req, res) => {
const { user, password } = req.body;
const queryString = "SELECT * FROM users WHERE user_id = ?";
db.query(queryString, [user])
.then(result => {
if (result.length > 0) {
const hash = result[0].password;
//here bcrypt.compare works server side or with cURL but won't set the response headers in the browser
bcrypt
.compare(password, hash)
.then(same => {
if (same === true) {
console.log("correct password");
res.status(200).json({
code: 200,
message: "correct password"
});
} else {
res.status(401).json({
code: 401,
message: "incorrect password"
});
}
})
.catch(err => console.log(err));
} else {
//this one works though and i can get the response in the browser so it can't be a cors issue
console.log("user does not exist");
res.status(200).json({
code: 401,
message: "User does not exist"
});
}
})
.catch(err => {
console.log("error" + err.message);
});
});
这是我在 react 中使用的测试函数
const signIn = () => {
fetch("http://localhost:5000/api/signIn", {
method: "POST",
body: JSON.stringify({
user: userName,
password: password
}),
headers: {
"Content-Type": "application/json"
},
})
.then(res => res.json())
.then(response => alert(response.code + response.message))
.catch(err => alert(err));
};
所以如果我输入了不在数据库中的错误用户名,警报功能会显示(code401User 不存在)但是如果我输入正确的用户 bcrypt.compare() 似乎没有设置正确和错误密码的响应,我会得到(TypeError:无法获取)。不过在 cURL 中测试 api 是可行的。
知道了,我忘了把 event.preventDefault() 放在 fetch 函数上。