leaky-ships/leaky-ships/lib/backend/components/checkPasswordIsValid.ts
2023-04-10 17:06:38 +02:00

19 lines
538 B
TypeScript

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