Node.js 6.x Blueprints
上QQ阅读APP看书,第一时间看更新

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);