[英]Password hashing not working on Put request
Using bcryptjs library and got 2 issues when performing Put request:使用 bcryptjs 库并在执行 Put 请求时遇到 2 个问题:
1- On insomnia / postman, server returns hashed password, but it returns "unhashed" on client side inspect console tools. 1- 在失眠/postman 上,服务器返回散列密码,但在客户端检查控制台工具上返回“未散列”。
2- I can not perform a login with new password credential. 2- 我无法使用新密码凭证登录。
Any visible syntax mistake on server code or quick suggestions please?服务器代码上有任何明显的语法错误或快速建议吗? Tks!谢!
CLIENT SIDE CODE:客户端代码:
function DevUpdateForm() {
const [name, setName] = useState("");
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [techs, setTechs] = useState("");
async function updateUser(e) {
e.preventDefault()
const dataUpdate = { name, email, password, techs}
const response = await api.put(`/devs/${dev._id}`, dataUpdate);
console.log(response);
setName('');
setEmail('');
setPassword('');
setTechs([]);
}
return (
<div className="update-profile">
<strong>Alterar</strong>
<img src={dev.avatar_url} alt={dev.name}/>
<form onSubmit={updateUser} >
<div className="input-block">
<label htmlFor="name">Nome:</label>
<input
name="name"
id="name"
type="text"
placeholder={"Alterar nome"}
value={name}
onChange={e => setName(e.target.value)}
/>
</div>
<div className="input-block">
<label htmlFor="email">E-mail:</label>
<input
name="email"
id="email"
type="text"
placeholder={"Alterar email"}
value={email}
onChange={e => setEmail(e.target.value)}
/>
</div>
<div className="input-block">
<label htmlFor="password">Senha:</label>
<input
name="password"
id="password"
type="password"
placeholder={"Digite nova senha"}
value={password}
onChange={e => setPassword(e.target.value)}
/>
</div>
<div className="input-block">
<label htmlFor="techs">Techs:</label>
<input
name="techs"
id="techs"
type="text"
placeholder={"Alterar tecnologias"}
value={techs}
onChange={e => setTechs(e.target.value)}
/>
</div>
<button type="submit" >Salvar</button>
</form>
</div>
)
}
export default DevUpdateForm;
SERVER SIDE UPDATE CONTROLLER:服务器端更新 CONTROLLER:
async update(req, res, next) {
try {
const { name, email, password, techs } = req.body;
const { _id } = req.params;
const techsArray = parseStringAsArray(techs);
let dev = await Dev.findByIdAndUpdate(_id, {
name,
email,
password,
techs: techsArray
},
{new: true});
dev = await Dev.findById(_id)
dev.password = await bcrypt.hash(dev.password, 8)
if (dev !== null) {
res.status(200).send({
data: dev,
message: "Dev has been updated!"
});
} else {
res.status(404).end();
}
} catch (error) {
next(error)
}
},
ROUTE:路线:
routes.put('/devs/:_id', DevController.allowIfLoggedin, DevController.grantAccess('updateAny', 'profile'), DevController.update);
After finding the model and set new values to user model.找到 model 并为用户 model 设置新值后。 you have to execute .save() statement to update existing changes.您必须执行.save()语句来更新现有更改。
async update(req, res, next) {
try {
const { name, email, password, techs } = req.body;
const { _id } = req.params;
const techsArray = parseStringAsArray(techs);
let dev = await Dev.findByIdAndUpdate(_id, {
name,
email,
password,
techs: techsArray
},
{new: true});
dev = await Dev.findById(_id)
dev.password = await bcrypt.hash(dev.password, 8)
dev.save();// add this to save new password
if (dev !== null) {
res.status(200).send({
data: dev,
message: "Dev has been updated!"
});
} else {
res.status(404).end();
}
} catch (error) {
next(error)
}
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.