라이브러리 설치
$ npm i bcrypt
$ npm i -D @types/bcrypt
비밀번호 암호화하기
async signUp(signUpRequestDto: SignUpRequestDto) {
const { email, password } = signUpRequestDto;
const user = new User();
user.email = email;
const hashedPassword = await bcrypt.hash(password, 10);
user.password = hashedPassword;
return await this.userRepository.save(user);
}
saltround 를 .env 파일에서 호출하여 넣을 때
호출한 값의 데이터 타입 = string 이므로 아래와 같이 확인 절차를 걸쳐서 number 로 변환해주어야 한다.
const saltRounds = this.configService.get<string>('SALT_ROUNDS');
if (saltRounds === undefined) {
throw new Error('SALT_ROUNDS is not defined in the configuration.');
}
const hashedPassword = await bcrypt.hash(password, parseInt(saltRounds));
signIn 을 할때는 아래 코드를 이용하여 입력된 패스워드와 DB에 저장된 패스워드가 일치하는지 확인
const passwordMatch = await bcrypt.compare(password, user.password);
자료출처
https://docs.nestjs.com/security/encryption-and-hashing
'프로그래밍 > TypeScript' 카테고리의 다른 글
[ TS ] DTO object 구현 (0) | 2024.08.03 |
---|---|
[ TS ] 불변객체 immutable 장점 / 단점 / 구현방법 (0) | 2024.07.28 |
[ TS ] tsconfig.json 의 내용 정리 (0) | 2024.06.08 |
[ TS ] 오버로딩 (0) | 2024.05.09 |
[ TS ] Promise<void> 의미 (0) | 2024.05.07 |