How it works...
Our pre-save hook is called whenever we call the save method on our document. This hook operates very similarly to our express middleware, allowing us to transform the document before saving it to MongoDB. For password security, we use bcrypt to create a salted hash of the provided password and save that to the database, instead of the original string. This helps protects our user's privacy in the event of a security breach that had access to our database.
To compare the password, we simply added a new comparison function to generate a new hash with a provided password and compare the results. The actual internals of bcrypt are fairly complicated and are based on a JavaScript implementation of the blowfish encryption algorithm. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access