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

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); 
主站蜘蛛池模板: 南漳县| 读书| 嘉祥县| 京山县| 扬中市| 伊宁县| 新巴尔虎右旗| 耿马| 龙里县| 江永县| 贺州市| 合山市| 民乐县| 林西县| 漳浦县| 玉林市| 合水县| 德江县| 河曲县| 寿宁县| 乐陵市| 精河县| 卓尼县| 西乌珠穆沁旗| 阿拉尔市| 平潭县| 扎赉特旗| 贺兰县| 淳安县| 枣庄市| 弥渡县| 谢通门县| 松滋市| 林甸县| 织金县| 巴楚县| 织金县| 勐海县| 车致| 丹巴县| 潞城市|