npm install --save bcrypt

bcrypt로 암호화하기

import bcrypt from 'bcrypt';
const saltRounds = 10; // salt 설정
const myPlaintextPassword = 's0/\\/\\P4$$w0rD'; // 이런 비밀번호가 있다고 가정

bcrypt.hash(myPlaintextPassword, saltRounds, function(err, hash) {
    // 이곳에 DB 저장 밑 에러 처리 코드 적용
});

//or

bcrypt.hash(myPlaintextPassword, saltRounds).then(function(hash) {
    // Store hash in your password DB.
}); // then을 붙이면 bcrypt가 완료되면 그 결과 값을 받아 then에 넣은 함수를 바로 실행시킨다.

비밀번호를 검증 할 때는 다음과 같이 한다.

bcrypt.compare(유저가 입력한 비밀번호, DB에 저장된 암호화 비밀번호, function(err, result) {
    // result == true
});
bcrypt.compare(유저가 입력한 비밀번호, DB에 저장된 암호화 비밀번호).then(function(result) {
    // result == true
~~}~~);

참고링크

[Node.js / JWT] Express.js 서버에서 JWT 기반 회원인증 시스템 구현하기