如果我输入了错误的密码或错误的电子邮件,登录过程就会崩溃:无法在发送到客户端后设置标题



我的后端有问题。我不知道为什么这个应用程序很受欢迎。我不会在这里发送两次标题。也许你知道怎么解决这个问题?如果我输入了错误的密码或用户名,那就是破解。但如果不是错误的数据--->一切都很好。。

// Authentication
router.post("/login", async (req, res) => {
try{
const user = await User.findOne({username: req.body.username});
!user && res.status(401).json("User not found.");
const hashedPassword = CryptoJs.AES.decrypt(user.password, process.env.SECRET);
const originalPassword = hashedPassword.toString(CryptoJs.enc.Utf8);
originalPassword !== req.body.password && 
res.status(401).json(`Incorrect username or password`);
const token = jwt.sign({
id: user.id,
isAdmin: user.isAdmin,
}, process.env.JWT_SECRET, { expiresIn: "3d" });
const {password, ...otherParams} = user._doc;
res.status(200).json({...otherParams, token});
} catch(e) {
res.status(500).json("ERROR ERROR ERROR " + e);
}
})

您应该在发送响应(return res.status()(后使用return来停止执行。所有res.status都在一个接一个地执行。只应发送一个响应。

相关内容

最新更新