1. 에러 원인
It happen because your password field on database have just a string, not a hashed string.
데이터베이스에 bcrypt 로 저장할때 hash string 이 아닌 일반 string 으로 저장이 되어서 발생하는 에러이다
2. 해결방법
hash 스트링 형태로 변형하여 넣으면 되는데
나의 경우에는 아래와 같이 했다.
1) bycrypt 모듈 설치
npm install bcryptjs
2) gensalt 로 salt 값 생성 후, 그 값을 hash 로 넘겨서 hash string 로 만들어 저장
bcrypt.genSalt(saltRounds, function(err, salt) {
bcrypt.hash(myPlaintextPassword, salt, function(err, hash) {
// Store hash in your password DB.
});
});
그러면 로그인 시 에러가 발생하지 않는다
3) 로그인 시에는 bscrypt.compare( ) 로 입력한 비밀번호와 db 에 저장된 비밀번호를 복호화하여 비교한다.
if (user) {
if (user_id === client_id && bcrypt.compare(user_pw, client_pw)) {
return { success:true }
}
return { success: false, msg : "비밀번호가 틀렸습니다."};
}
3. 참고 링크
Getting "Error: the string "Not a valid BCrypt hash." was thrown, throw an Error :)" during Mocha ExpressJS testing
I have a MEAN stack app that is using Passport for authentication. I'm trying to write a unit test that logs in and checks whether you are redirected to the root (/). However, whenever I run Mocha...
stackoverflow.com
[Node JS] Bcrypt를 이용하여 로그인 정보 암호화하기
지난 postman 포스팅에서 POSTMAN을 통해 데이터베이스에 등록한 내역은 아래와 같다. 아래 사진에서와 같이 비밀번호가 보이도록 저장해두면, 보안성이 떨어진다. 따라서 우리는 비밀번호 암호화
velog.io