Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

update password before saving to mongodb

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    bcrypt = require('bcrypt'),
    SALT_WORK_FACTOR = 10;
     
var UserSchema = new Schema({
    username: { type: String, required: true, index: { unique: true } },
    password: { type: String, required: true }
});
     
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 = mongoose.model('User', UserSchema);
Comment

update password before saving to mongodb

var mongoose = require(mongoose),
    User = require('./user-model');
     
var connStr = 'mongodb://localhost:27017/mongoose-bcrypt-test';
mongoose.connect(connStr, function(err) {
    if (err) throw err;
    console.log('Successfully connected to MongoDB');
});
     
// create a user a new user
var testUser = new User({
    username: 'jmar777',
    password: 'Password123'
});
     
// save the user to database
testUser.save(function(err) {
    if (err) throw err;
});
    
// fetch the user and test password verification
User.findOne({ username: 'jmar777' }, function(err, user) {
    if (err) throw err;
     
    // test a matching password
    user.comparePassword('Password123', function(err, isMatch) {
        if (err) throw err;
        console.log('Password123:', isMatch); // -> Password123: true
    });
     
    // test a failing password
    user.comparePassword('123Password', function(err, isMatch) {
        if (err) throw err;
        console.log('123Password:', isMatch); // -> 123Password: false
    });
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript class in external file 
Javascript :: change the focus to next in angular forms 
Javascript :: js loop array back 
Javascript :: Looping arrays with for loop 
Javascript :: change h2 to h1 using javascript 
Javascript :: Check if a number is even or odd 
Javascript :: ex: javascript loop array 
Javascript :: call function add parameter javascript 
Javascript :: declaring variable react hooks 
Javascript :: public JsonResult what is the return 
Javascript :: windows 10 retiré le theme sombre explorateur 
Javascript :: node js write read string to file 
Python :: All caps alphabet as list 
Python :: django EMAIL_BACKEND console 
Python :: sqlalchemy python install 
Python :: install matplotlib 
Python :: transform size of picture pygame 
Python :: save thing in pickle python 
Python :: python argparse ignore unrecognized arguments 
Python :: python start simplehttpserver 
Python :: how to get the url of the current page in selenium python 
Python :: truncate templat tag django 
Python :: conda create environment 
Python :: python urlencode 
Python :: python read file to variable 
Python :: python save figure 
Python :: python list of all states 
Python :: rgb to grayscale python opencv 
Python :: save plot as pdf python 
Python :: django import Q 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =