[TIL] Sequelize와 bcrypt

김민재·2023년 11월 10일
0

TIL

목록 보기
46/172

// 데이터베이스 안에는 회원가입 시 비밀번호가 같아도 해쉬는 랜덤이다

  1. 회원가입 시 데이터베이스 안에 해쉬 된 비밀번호를 넣어준다.
const hash_password = await bcrypt.hash(password, 10);
// req.bdoy.password를 해쉬시켜준다
    const check = await bcrypt.compare(password, hash_password);
    console.log(check);
    // 불리언반환
    await Signs.create({
      email,
      password: hash_password,
      // 값을 넣어준다.
      nickname,
    });

    res.status(201).send("회원가입 완료");
  1. 로그인 시 bcrypt 해쉬와 유저 해쉬를 비교해준다.
 const { password, email } = req.body;

    const user = await Signs.findOne({
      where: { email: email },
    }); //같은 이메일을 찾아주고
    const isPasswordValid = await bcrypt.compare(password, user.password);
    // req.body.password와 user.password를 비교해준다. 불리언반환
    if (!isPasswordValid) { // 틀리다면 에러
      return res.status(400).send("비밀번호를 잘못 입력하셨습니다.");
    }

    if (user.length === 0) { // 같은 이메일 회원이 없다면 
      return res.status(400).send("회원이 아닙니다.");
    }
	// isPasswordValid 로그인 실행
    res.status(201).send("로그인 완료");
  } catch (error) {
    console.log(error);
  }
profile
개발 경험치 쌓는 곳

0개의 댓글

관련 채용 정보