As we mentioned before, bcryptjs is used to hash and validate passwords. Please take a look at the simplified steps for further advice on how to use this module in our app:
- Install the bcryptjs module via npm:
$ npm i bcryptjs
- Hash a password by adding salt with the password sent from the client in the request body (request), for example, during a new user creation in the user module:
// src/modules/public/user/_routes/create-user.js
import bcrypt from 'bcryptjs'
const saltRounds = 10
const salt = bcrypt.genSaltSync(saltRounds)
const hashed = bcrypt.hashSync(request.password, salt)
Note that to speed up our authentication lesson in the chapter, we skip the process of creating a new user. But in a more complete CRUD, you can use this step to hash the password provided by the user.
- Verify a password by comparing the password sent from the client (request) with the one stored in the database, for example, during the login authentication...