我正在尝试使用react和mongoDB创建登录表单
这是我的控制器:
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
这是我的LoginForm.jsx:
authenticate: function (req, res, next) {
Admin.findOne({
username: req.body.username
}, function (err, adminInfo) {
if (err) {
next(err);
} else {
if (bcrypt.compareSync(req.body.password, adminInfo.password)) {
const token = jwt.sign({
id: adminInfo._id
},
req.app.get("secretKey"), {
expiresIn: "1h"
}
);
res.json({
status: "success",
message: "admin found!!!",
data: {
user: adminInfo,
token: token
},
});
} else {
res.json({
status: "error",
message: "Invalid email/password!!!",
data: null,
});
}
}
});
}
单击按钮时如何验证用户名和密码? 我试图输入错误的密码和错误的用户名,但仍然显示“ Hello” 我该如何解决?
谢谢
如果您想查看我的代码,这是我的github项目: https://github.com/nathannewyen/the-beuter
答案 0 :(得分:1)
无论api调用的结果如何,您似乎都调用了“ setRouteRedirect(true)”。
如果结果令人满意,您应该在帖子的then函数中设置RouteRedirect。
此通话
axios
.post("/login/authenticate", admin)
.then((res) => {
if (res.data.errors) {
setError(true);
} else {
return null;
}
})
.catch((err) => console.log(err));
不阻塞代码,因此在发送请求后,将调用setRouteRedirect。