问题描述
我不是后端专家 - 只知道一些简单的事情。我将 Express 和 Postgresql 与 Knex 查询生成器一起使用。
我的数据库有用户表,我创建了客户端 (React) 重置密码功能。触发更新密码,我可以使用更新的密码登录。此外,当我使用 DBeaver 检查我的数据库时,我会看到更新的密码。
这是我的后端更新密码路径:
var express = require('express');
var router = express.Router();
const bcrypt = require('bcrypt');
const BCRYPT_SALT_ROUNDS = 12;
module.exports = knex => {
router.put('/updatePasswordViaEmail',(req,res) => {
knex
.select("*")
.from('users')
.where('full_name','=',req.body.username)
.then(user => {
//console.log('user ',user)
if (user.length !== 0) {
console.log('user exists in db');
bcrypt
.hash(req.body.password,BCRYPT_SALT_ROUNDS)
.then(hashedPassword => {
knex('users')
.where('full_name',req.body.username)
.update({
password: hashedPassword,resetPasswordToken: null,resetPasswordExpires: null,})
.then((response) =>{
res.send(response)
console.log('password updated!')
}).catch((err)=>{
console.log('password Could not be updated',err)
})
})
.then(() => {
console.log('password updated');
res.status(200).send({ message: 'password updated' });
});
} else {
console.error('no user exists in db to update');
res.status(401).json('no user exists in db to update');
}
});
});
return router;
};
但是当我重置数据库 (knex migrate:rollback && npm run db:migrate && npm run db:seed
) 时,新密码回滚到默认值是合乎逻辑的,因为所有默认值都在 seed.js 文件中,那么如何保持更新数据库重置后的密码?
我使用迁移文件进行了操作:尝试不触摸密码,但没有用。也许有一种方法可以将更新后的密码直接存储到seed.js 文件中,这样重置不会影响新密码?
我对这个问题感到困惑 =( 也许我的逻辑不正确,或者我不明白一些简单的事情?感谢您的帮助。在此先感谢您。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)