jaumard/trailpack-passport

View on GitHub
api/models/hashPassword.js

Summary

Maintainability
A
45 mins
Test Coverage
/**
 * Hash a passport password.
 *
 * @param bcrypt implementation
 * @param {Object}   passport
 * @param {Function} next
 */
module.exports = (bcrypt, passport, next) => {
  if (passport.password) {
    if (next) {
      bcrypt.hash(passport.password, 10, (err, hash) => {
        passport.password = hash
        next(err, passport)
      })
    }
    else {
      return bcrypt.hash(passport.password, 10).then(hash =>
        passport.password = hash
      )
    }
  }
  else {
    if (next) {
      next(null, passport)
    }
    else {
      return Promise.resolve(passport)
    }
  }
}