Search code examples
javascriptbcryptmodularity

Reusable bcrypt function, returning data from function issue


I have this hashing function where I would like to get back all hashes that are made using bcryptjs. When I console.log(encrypt(password)) it returns undefined. I've attempted to do a Promise.all but I'm not very good with promises. Considering the fact it returns undefined my guess would be that it is the way the function is structured.

function

const bcrypt = require("bcryptjs");

module.exports = function encrypt(password) {
    bcrypt.genSalt(10, async (err, salt) => {
      const encryptedPassword = await bcrypt.hash(password, salt);
      const details = {
        password: encryptedPassword,
      };
      return details;
    });
}; 

Let me know if you need anything else from me.


Solution

  • Using async and await like this, for encrypted password

    const bcrypt = require("bcryptjs");        
    module.exports = async function encryptPassword(password) { 
        return await bcrypt.hash(password, 10)
    };