import bcrypt from 'bcrypt'; import logger from './logger'; const SALT_ROUNDS = 12; /** * Hash a password using bcrypt * @param password - Plain text password to hash * @returns Hashed password */ export const hashPassword = async (password: string): Promise => { try { const salt = await bcrypt.genSalt(SALT_ROUNDS); const hashedPassword = await bcrypt.hash(password, salt); return hashedPassword; } catch (error) { logger.error('Error hashing password', { error: error instanceof Error ? error.message : 'Unknown error', }); throw new Error('Failed to hash password'); } }; /** * Compare a plain text password with a hashed password * @param password - Plain text password to compare * @param hash - Hashed password to compare against * @returns True if passwords match, false otherwise */ export const comparePassword = async ( password: string, hash: string ): Promise => { try { const isMatch = await bcrypt.compare(password, hash); return isMatch; } catch (error) { logger.error('Error comparing passwords', { error: error instanceof Error ? error.message : 'Unknown error', }); throw new Error('Failed to compare passwords'); } };