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

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); 
主站蜘蛛池模板: 嘉峪关市| 行唐县| 衡水市| 彭泽县| 玉门市| 石楼县| 武川县| 崇义县| 志丹县| 蒙阴县| 昌邑市| 历史| 蕉岭县| 淳安县| 常德市| 鹤壁市| 北川| 太保市| 梓潼县| 宾阳县| 淮阳县| 鄂伦春自治旗| 大丰市| 芦山县| 崇明县| 潼关县| 乌鲁木齐县| 理塘县| 马鞍山市| 贡嘎县| 荔波县| 贺兰县| 铁力市| 县级市| 井研县| 曲靖市| 庆城县| 营山县| 江山市| 四平市| 洮南市|