Source

utils/password.utils.js

import bcrypt from "bcrypt";
import { env } from "../config/env.js";
/**
 * Utility functions for password operations.
 * @category Helpers
 */
export class PasswordUtils {
    /**
     * Hash a plain text password.
     * @param {string} password Plain text password.
     * @returns {Promise<string>} Hashed password.
     */
    static async hashPassword(password) {
        return await bcrypt.hash(password, env.BCRYPT_SALT_ROUNDS);
    }
    /**
     * Compare a plain text password with a hashed password.
     * @param {string} password Plain text password.
     * @param {string} hashedPassword Hashed password to compare against.
     * @returns {Promise<boolean>} True if passwords match.
     */
    static async comparePassword(password, hashedPassword) {
        return await bcrypt.compare(password, hashedPassword);
    }
    /**
     * Validate password strength (optional utility).
     * @param {string} password Password to validate.
     * @returns {boolean} True if password meets requirements.
     */
    static validatePasswordStrength(password) {
        // Minimum 8 characters, at least one uppercase, one lowercase, one number
        const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d@$!%*?&]{8,}$/;
        return passwordRegex.test(password);
    }
}