All files / src/auth password.service.ts

100% Statements 14/14
100% Branches 2/2
100% Functions 3/3
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 32 33 34 35 36 37 38 39 40 41 427x 7x     7x 53x     4x             3x             26x   26x 5x       26x 10x         26x            
import { Injectable } from "@nestjs/common";
import * as bcrypt from "bcrypt";
 
@Injectable()
export class PasswordService {
  private readonly saltRounds = 12;
 
  async hashPassword(password: string): Promise<string> {
    return bcrypt.hash(password, this.saltRounds);
  }
 
  async comparePassword(
    password: string,
    hashedPassword: string,
  ): Promise<boolean> {
    return bcrypt.compare(password, hashedPassword);
  }
 
  validatePasswordStrength(password: string): {
    isValid: boolean;
    errors: string[];
  } {
    const errors: string[] = [];
 
    if (password.length < 8) {
      errors.push("Password must be at least 8 characters long");
    }
 
    // ASCII 문자만 허용 (영문 대소문자, 숫자, 특수문자, 공백)
    if (!/^[a-zA-Z0-9!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?`~ ]+$/.test(password)) {
      errors.push(
        "Password can only contain English letters, numbers, and basic special characters",
      );
    }
 
    return {
      isValid: errors.length === 0,
      errors,
    };
  }
}