Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

mongoose encrypt password

import bcrypt from 'bcryptjs'

userSchema.pre('save', async function (next) {
  if (!this.isModified('password')) { // if the password is not changed we don't want to hash it again
    return next()
  }

  const salt = await bcrypt.genSalt(10) // creates salt
  this.password = await bcrypt.hash(this.password, salt) //generates hash and combines it with the salt
})
Source by www.dailydeveloping.com #
 
PREVIOUS NEXT
Tagged: #mongoose #encrypt #password
ADD COMMENT
Topic
Name
3+3 =