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

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); 
主站蜘蛛池模板: 博爱县| 嵩明县| 乌恰县| 阿鲁科尔沁旗| 清镇市| 石家庄市| 高密市| 日照市| 涪陵区| 新民市| 定结县| 齐齐哈尔市| 阜城县| 广东省| 平武县| 金坛市| 万安县| 汤阴县| 天柱县| 勃利县| 公安县| 土默特右旗| 突泉县| 彰化市| 桐庐县| 灌南县| 闵行区| 阿拉善盟| 海门市| 吉林省| 全南县| 巴林左旗| 松阳县| 泽州县| 建平县| 贺兰县| 普洱| 古浪县| 庄浪县| 赣州市| 绥中县|