Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Importing modules
- var mongo = require("mongoose");
- var Schema = mongo.Schema;
- var bcrypt = require('bcrypt-nodejs');
- var User = require("./User");
- var UserSchema = new Schema({
- username: {
- type: String,
- required: true,
- unique: true
- },
- password: {
- type: String,
- required: true
- },
- email: {
- type: String,
- required: true
- },
- loginDate: {
- type: Date,
- default: Date.now
- }
- });
- UserSchema.pre('save', function(next) {
- var user = this;
- // only hash the password if it has been modified (or is new)
- if (!user.isModified('password'))
- return next();
- // generate a salt
- bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
- if (err)
- return next(err);
- // hash the password using our new salt
- bcrypt.hash(user.password, salt, function(err, hash) {
- if (err)
- return next(err);
- // override the cleartext password with the hashed one
- user.password = hash;
- next();
- });
- });
- });
- UserSchema.methods.comparePassword = function(candidatePassword, cb) {
- bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
- if (err)
- return cb(err);
- cb(null, isMatch);
- });
- };
- module.exports = mongo.model("Login", UserSchema);
Add Comment
Please, Sign In to add comment