노드 bcrypt사용법

안녕하세요·2023년 11월 16일

react x node 

목록 보기
7/18

노드 폴더에서 npm install bcrypt 하여 설치

  const hashPass = bcrypt.hashSync(pass, 10);

bcrypt를 사용하여 해쉬패스워드 생성

export async function getLogin(id) {
  const sql = `select count(pass) as cnt ,ANY_VALUE(pass) as pass from shoppy_member where id = ? ; ` // 로그인 진행시 count 함수를 사용
  return db
    .execute(sql, [id])
    .then((row) => row[0][0]);

}

로그인시 MySQL count함수를 활용해 아이디가 있는지 확인

export async function getLogin(req, res) {
  const { id, pass } = req.body;
  const result = await memberRepository.getLogin(id);
  
  if (result.cnt === 1) {

    if (await bcrypt.compare(pass, result.pass)) {
      result.login_result = true
    } else {
      result.login_result = false
    }
  } else {
    result.login_result = false
  }
  console.log(result);
  res.json(result)
};

post 받은 패스워드를 bcript화 하여 회원정보 비교 후 결과를 json 형태로 전송

0개의 댓글