Avoid user from setting the same old password

This commit is contained in:
Ephraim Atta-Duncan
2023-06-05 16:36:16 +00:00
parent 4136811e32
commit 2b9a2ff250
3 changed files with 29 additions and 3 deletions

View File

@ -38,6 +38,24 @@ export default function ResetPassword(props: any) {
} }
); );
if (!response.ok) {
toast.dismiss();
if (response.status == 404) {
toast.error("Invalid Token");
}
if (response.status == 400) {
toast.error("New password must be different");
}
if (response.status == 500) {
toast.error("Something went wrong.");
}
return;
}
if (response.ok) { if (response.ok) {
setResetSuccessful(true); setResetSuccessful(true);
setTimeout(() => { setTimeout(() => {

View File

@ -1,5 +1,5 @@
import { NextApiRequest, NextApiResponse } from "next"; import { NextApiRequest, NextApiResponse } from "next";
import { sendResetPassword, sendResetPasswordSuccessMail } from "@documenso/lib/mail"; import { sendResetPassword } from "@documenso/lib/mail";
import { defaultHandler, defaultResponder } from "@documenso/lib/server"; import { defaultHandler, defaultResponder } from "@documenso/lib/server";
import prisma from "@documenso/prisma"; import prisma from "@documenso/prisma";
import crypto from "crypto"; import crypto from "crypto";

View File

@ -1,5 +1,5 @@
import { NextApiRequest, NextApiResponse } from "next"; import { NextApiRequest, NextApiResponse } from "next";
import { hashPassword } from "@documenso/lib/auth"; import { hashPassword, verifyPassword } from "@documenso/lib/auth";
import { sendResetPasswordSuccessMail } from "@documenso/lib/mail"; import { sendResetPasswordSuccessMail } from "@documenso/lib/mail";
import { defaultHandler, defaultResponder } from "@documenso/lib/server"; import { defaultHandler, defaultResponder } from "@documenso/lib/server";
import prisma from "@documenso/prisma"; import prisma from "@documenso/prisma";
@ -22,7 +22,15 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
}); });
if (!foundToken) { if (!foundToken) {
return res.status(400).json({ message: "Invalid token." }); return res.status(404).json({ message: "Invalid token." });
}
const isSamePassword = await verifyPassword(password, foundToken.User.password!);
if (isSamePassword) {
return res
.status(400)
.json({ message: "New password must be different from the current password." });
} }
const hashedPassword = await hashPassword(password); const hashedPassword = await hashPassword(password);