import { rejectionErrors } from "../errors"
import sendError, { API } from "./sendError"
import type { Player } from "@prisma/client"
import bcrypt from "bcrypt"

export default async function checkPasswordIsValid<T>(
  context: API<T>,
  player: Player,
  password: string
) {
  // Validate for correct password
  const result = await bcrypt.compare(password, player.passwordHash ?? "")
  if (!result) throw sendError(context, rejectionErrors.wrongPassword)

  return
}
