02 - bcrypt setup
In this module we'll set up bcrypt.js
in our application.
Import
We already have the bcryptjs
package in our package.json
file. Once installed, we have to add it to the database. Just like with jwt
, create a new variable inside of usercontroller.js
. For its declaration, use the require
statement for bcryptjs
.
Adding bcrypt
Let's add bcrypt into the create method with the pass
value for the passwordhash
property:
Examining bcrypt.hashSync()
Here's a short diagram to give you an overview:
We're adding the hashSync()
function to our new User object so that we don't store the password in a format that is easy to read. In our code, we supply the original password and tell bcrypt to use the salt 10 times.
Postman and Postgres
Test in Postman:
Test in Postgres:
Conclusion
At the moment, we have the ability to add new users to our database. Unfortunately, those users currently do not have a way back in after the initial signup, which defeats the purpose of having an account. In the next chapter, we'll setup a login route for existing users to access the database with their credentials.
Last updated