久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

使用mongoose和bcrypt怎么實現一個用戶密碼加密功能

189次閱讀
沒有評論

共計 3275 個字符,預計需要花費 9 分鐘才能閱讀完成。

自動寫代碼機器人,免費開通

本篇文章給大家分享的是有關使用 mongoose 和 bcrypt 怎么實現一個用戶密碼加密功能,丸趣 TV 小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著丸趣 TV 小編一起來看看吧。

使用 npm 安裝即可

npm install --save bcrypt

用戶模型

下面來創建代碼用戶 user 的 schema,用戶名不能重復

var mongoose = require(mongoose),
 Schema = mongoose.Schema,
 bcrypt = require( bcrypt var UserSchema = new Schema({ username: { type: String, required: true, index: { unique: true } },
 password: { type: String, required: true }
module.exports = mongoose.model(User , UserSchema);

加密

下面加入用戶模型的是 Mongoose 的中間件,該中間件使用 pre 前置鉤子,在密碼保存之前,自動地把密碼變成 hash。詳細代碼如下

let SALT_WORK_FACTOR = 5
UserSchema.pre(save , function(next) {
 var user = this;
 // 產生密碼 hash 當密碼有更改的時候 (或者是新密碼)
 if (!user.isModified( password)) return next();
 //  產生一個 salt
 bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) { if (err) return next(err);
 //  結合 salt 產生新的 hash
 bcrypt.hash(user.password, salt, function(err, hash) { if (err) return next(err);
 //  使用 hash 覆蓋明文密碼
 user.password = hash;
 next();
 });
 });
});

在 node.bcrypt.js 中 SALT_WORK_FACTOR 默認使用的是 10,這里設置為 5

驗證

加密之后,密碼原文被替換為密文了。我們無法解密,只能通過 bcrypt 的 compare 方法,對再次傳入的密碼和數據庫中保存的加密后的密碼進行比較,如果匹配,則登錄成功

UserSchema.methods.comparePassword = function(candidatePassword, cb) { bcrypt.compare(candidatePassword, this.password, function(err, isMatch) { if (err) return cb(err);
 cb(null, isMatch);
 });
};

把上面的幾個步驟串在一起,完整代碼如下

var mongoose = require(mongoose),
 Schema = mongoose.Schema,
 bcrypt = require(bcrypt),
 SALT_WORK_FACTOR = 5;
var UserSchema = new Schema({ username: { type: String, required: true, index: { unique: true } },
 password: { type: String, required: true }
UserSchema.pre(save , function(next) {
 var user = this;
 // only hash the password if it has been modified (or is new)
 if (!user.isModified( password)) return next();
 // generate a salt
 bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) { if (err) return next(err);
 // hash the password using our new salt
 bcrypt.hash(user.password, salt, function(err, hash) { if (err) return next(err);
 // override the cleartext password with the hashed one
 user.password = hash;
 next();
 });
 });
UserSchema.methods.comparePassword = function(candidatePassword, cb) { bcrypt.compare(candidatePassword, this.password, function(err, isMatch) { if (err) return cb(err);
 cb(null, isMatch);
 });
module.exports = mongoose.model(User , UserSchema);

測試

把上面的代碼保存成 user-model.js,然后運行下面代碼來實際測試

var mongoose = require(mongoose),
 User = require( ./user-model 
var connStr =  mongodb://localhost:27017/mongoose-bcrypt-test 
mongoose.connect(connStr, function(err) { if (err) throw err;
 console.log( Successfully connected to MongoDB 
// create a user a new user
var testUser = new User({
 username:  jmar777 ,
 password:  Password123 
// save user to database
testUser.save(function(err) { if (err) throw err;
 // fetch user and test password verification
 User.findOne({ username:  jmar777  }, function(err, user) { if (err) throw err;
 // test a matching password
 user.comparePassword(Password123 , function(err, isMatch) { if (err) throw err;
 console.log(Password123: , isMatch); // -  Password123: true
 });
 // test a failing password
 user.comparePassword(123Password , function(err, isMatch) { if (err) throw err;
 console.log(123Password: , isMatch); // -  123Password: false
 });
 });
});

控制臺中輸入如下數據:

使用 mongoose 和 bcrypt 怎么實現一個用戶密碼加密功能

數據庫數據如下:

使用 mongoose 和 bcrypt 怎么實現一個用戶密碼加密功能

以上就是使用 mongoose 和 bcrypt 怎么實現一個用戶密碼加密功能,丸趣 TV 小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注丸趣 TV 行業資訊頻道。

向 AI 問一下細節

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-12-04發表,共計3275字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 平山县| 慈溪市| 马关县| 铁力市| 信丰县| 东至县| 北宁市| 洮南市| 翁源县| 大名县| 徐汇区| 遂川县| 长垣县| 澳门| 高唐县| 凌云县| 读书| 陈巴尔虎旗| 安仁县| 合作市| 修武县| 福鼎市| 聂拉木县| 长春市| 汾阳市| 利川市| 株洲县| 海丰县| 兴义市| 逊克县| 连平县| 柳州市| 晋宁县| 清河县| 大荔县| 长宁县| 和政县| 承德市| 柳江县| 和平县| 曲水县|