Search
 
SCRIPT & CODE EXAMPLE
 

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
})
Comment

PREVIOUS NEXT
Code Example
Javascript :: js object.entries with sort 
Javascript :: javascript add item to list 
Javascript :: js get screen width 
Javascript :: remove an element from array javascript 
Javascript :: mysql_real_escape_string for nodejs 
Javascript :: react router reload page not found 
Javascript :: end session express 
Javascript :: what are closures 
Javascript :: destructuring an object js 
Javascript :: function for flatten an array 
Javascript :: why app.use(cors()) not workin 
Javascript :: js remove escape characters from json 
Javascript :: then and catch in promise 
Javascript :: html form data to json 
Javascript :: a href javascript 
Javascript :: Javascript number Count up 
Javascript :: react tailwind loading 
Javascript :: delete node from linked list 
Javascript :: what is $ in jquery 
Javascript :: redux saga use navigation 
Javascript :: spinner react native 
Javascript :: pull out only text from element javascript 
Javascript :: import javascript 
Javascript :: export aab bundle react native android 
Javascript :: axios delete request 
Javascript :: set property js 
Javascript :: how to delete everything from a folder in js 
Javascript :: how to check for unused dependencies in my react project 
Javascript :: js check string is date 
Javascript :: sum array without loop javascript 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =