carlosorjuela / RecipeChallenge

Recipe challenge from Puzzle Digital
0 stars 0 forks source link

Bcrypt. Sync #2

Open juanibe opened 3 years ago

juanibe commented 3 years ago
   await bcrypt.hash(variables.password, 10).then(function(hash){
            newUser.password = hash
        })

"Bcrypt supports both sync and async methods. The asynchronous approach is recommended because hashing is CPU intensive, and the synchronous version will block the event loop and prevent your app from handling other requests until it finishes."

Your intention is ok, since you decided to make the async method. However there are two main mistakes: First, node improves the performance, making javascript methods asynchronous, remember that JS is sync. To do that node, use callbacks. So:

bcrypt.hash('myPassword', 10, function(err, hash) {
  // Store hash in database
});

Second, you are mixing the async/await approach with the then() promised based one.

aync function(){ 
const hello = await {...}
}

Since it is in register method and the call is not very often you can use the sync method to encrypt the password

let hash = bcrypt.hashSync('myPassword', 10);
// Store hash in database
carlosorjuela commented 3 years ago

Ok got it.. thanks