import * as bcrypt from "bcryptjs"

export const runtime = "nodejs"

const saltRounds: number = 10

export async function verifyPassword(
  plainPassword: string,
  hashedPassword: string,
): Promise<boolean> {
  const isMatch = await bcrypt.compare(plainPassword, hashedPassword)
  if (!isMatch) {
    return false
  }
  return isMatch
}

export async function getPasswordHash(password: string): Promise<string> {
  if (!password) {
    throw new Error("Password is required")
  }
  const salt = await bcrypt.genSalt(saltRounds)
  const hash = await bcrypt.hash(password, salt)
  return hash
}
