Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

Mini

[Nest JS] 비밀번호 암호화 구현 본문

JS/Nest.js

[Nest JS] 비밀번호 암호화 구현

Mini_96 2024. 8. 18. 21:22

npm install bcryptjs --save

 

* salt => 같은비밀번호를 입력해도 다른 해시값이 저장되도록

import * as bcrypt from 'bcryptjs'

@Injectable()
export class UserRepository extends Repository<User>{
  ...
  async createUser(authCredentialsDto : AuthCredentialsDto){
    const {username, password} = authCredentialsDto;

    const salt = await bcrypt.genSalt();
    const hashedPassword = await bcrypt.hash(password, salt);

    const user = this.create({username,password : hashedPassword}); //리포지토리.create => db저장용객체로 만들어줌

    try {
      await this.save(user);
    ...
}

 

 

결과

 

* 로그인도 수정

if(!await bcrypt.compare(password, findMember.password)){
    console.log('비밀번호 불일치');
    return "redirect:user/login_failed";
}