leaky-ships/leaky-ships/lib/backend/components/checkPasswordIsValid.ts
2023-04-10 22:08:16 +02:00

17 lines
493 B
TypeScript

import type { Player } from "@prisma/client"
import bcrypt from "bcrypt"
import { rejectionErrors } from "../errors"
import sendError, { API } from "./sendError"
export default async function checkPasswordIsValid(
context: API,
player: Player,
password: string,
next: () => void
) {
// Validate for correct password
const result = await bcrypt.compare(password, player.passwordHash ?? "")
if (!result) return sendError(context, rejectionErrors.wrongPassword)
return next()
}