gpt4 book ai didi

javascript - Bookshelf.js 正在使用 bcrypt 更改每次数据更新时的密码

转载 作者:行者123 更新时间:2023-11-30 15:14:21 25 4
gpt4 key购买 nike

我的用户模型是

var Bookshelf = require('../../db').bookshelf;
var bcrypt = require('bcrypt');
var Promise = require('bluebird');
var Base = require('./../helpers/base');

// User model
var User_Model = Bookshelf.Model.extend({
tableName: 'users',
initialize: function() {
this.on('saving', this.hashPassword, this);
},
hashPassword: function(model, attrs, options) {
return new Promise(function(resolve, reject) {
bcrypt.hash(model.attributes.password, 10, function(err, hash) {
if( err ) reject(err);
model.set('password', hash);
resolve(hash); // data is created only after this occurs
});
});
}
});

创建用户时密码设置为

$2a$10$QD6FmChgGZEMooCklLfemOvAaHHV2RfUMQ5EoNwuMeojb6y2Hh2iK

并且在登录时完美运行,

当对用户字段进行任何更新时,都会导致密码每次更改为随机值

$2a$10$IVW.2JN/TPVABsx73t870uwKMNsDKoGmLJzOs1y/MPMOe.WRMBL4W

$2a$10$12sjEqM3t345dcq7rDixgu.9TZiWOC.JN5Ke8SI2F0IXv/YpdRwTK

最佳答案

因为你使用的是 bookshelfjs。您可以使用 Bookshelf-bycrypt plugin它负责散列密码。更新表行时明确提及更新如下

model.save([key], [val], [attrs], [options])


// update authors set "bio" = 'Short user bio' where "id" = 1
new Author({id: 1, first_name: 'User'})
.save({bio: 'Short user bio'}, {method: "update" }{patch: true},)
.then(function(model) {
// ...
});

关于javascript - Bookshelf.js 正在使用 bcrypt 更改每次数据更新时的密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44686471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com