官术网_书友最值得收藏!

  • Node.js 6.x Blueprints
  • Fernando Monteiro
  • 104字
  • 2021-07-14 10:35:01

Creating a models folder and adding a user schema

Create a models folder inside server/ and add the following code:

// Import Mongoose and password Encrypt 
var mongoose = require('mongoose'); 
var bcrypt   = require('bcrypt-nodejs'); 
 
// define the schema for User model 
var userSchema = mongoose.Schema({ 
    // Using local for Local Strategy Passport 
    local: { 
        name: String, 
        email: String, 
        password: String, 
    } 
 
}); 
 
// Encrypt Password 
userSchema.methods.generateHash = function(password) { 
    return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null); 
}; 
 
// Verify if password is valid 
userSchema.methods.validPassword = function(password) { 
    return bcrypt.compareSync(password, this.local.password); 
}; 
 
// create the model for users and expose it to our app 
module.exports = mongoose.model('User', userSchema); 
主站蜘蛛池模板: 湘西| 贵州省| 济宁市| 祁连县| 叶城县| 石棉县| 泗水县| 榆树市| 沙河市| 包头市| 沁水县| 陵川县| 西充县| 荆门市| 安徽省| 城固县| 南阳市| 溧水县| 库车县| 林芝县| 沭阳县| 青川县| 闻喜县| 柏乡县| 枣庄市| 宣汉县| 乌兰县| 来安县| 太康县| 平利县| 海兴县| 九台市| 兴国县| 衡东县| 宁远县| 株洲县| 许昌县| 临武县| 鹤岗市| 同心县| 中山市|