[Node.js] mongoDB, bcrypt 이용한 로그인 구현

소영·2023년 1월 5일
0
exports.signin = async (req, res) => {
	const userid = req.body.userid;
    const userpw = req.body.userpw;
    
    const user = await User.findOne({ userid: userid });
    
    if (!user) {
    	/* 아이디(유저)가 존재하지 않을 때 코드 구현 */
    } else if (await bcrypt.compare(userpw, user.userpw)) {
    	/* 비밀번호가 일치할 때 코드 구현 (Login Success) */
    } else {
    	/* 비밀번호가 일치하지 않을 때 코드 구현 */
  • 로그인 기능 구현하면서 겪은 문제 *
else if(bcrypt.compare(userpw, user.userpw)) { /* ... */ }

처음엔 이렇게 작성했었는데 잘못된 비밀번호를 입력해도 자꾸 로그인 성공으로 인식해버렸다. (return값이 항상 true)
하지만 await을 붙여줘야 하는 것을 알았다!

⬇️ 참고한 사이트
https://stackoverflow.com/questions/62965515/node-bcrypt-compare-always-returns-success

0개의 댓글