mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 08:42:12 +10:00
Create reset password token for user
This commit is contained in:
@ -3,6 +3,7 @@ import { Button } from "@documenso/ui";
|
||||
import Logo from "./logo";
|
||||
import { ArrowLeftIcon } from "@heroicons/react/24/outline";
|
||||
import { FormProvider, useForm } from "react-hook-form";
|
||||
import { toast } from "react-hot-toast";
|
||||
|
||||
interface IResetPassword {
|
||||
email: string;
|
||||
@ -13,6 +14,21 @@ export default function ForgotPassword(props: any) {
|
||||
const { register, formState, resetField } = methods;
|
||||
|
||||
const onSubmit = async (values: IResetPassword) => {
|
||||
await toast.promise(
|
||||
fetch(`/api/auth/forgot-password`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(values),
|
||||
}),
|
||||
{
|
||||
loading: "Sending...",
|
||||
success: `Reset link sent. `,
|
||||
error: "Could not send reset link :/",
|
||||
}
|
||||
);
|
||||
|
||||
resetField("email");
|
||||
|
||||
console.log(values);
|
||||
|
||||
43
apps/web/pages/api/auth/forgot-password.ts
Normal file
43
apps/web/pages/api/auth/forgot-password.ts
Normal file
@ -0,0 +1,43 @@
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { defaultHandler, defaultResponder } from "@documenso/lib/server";
|
||||
import prisma from "@documenso/prisma";
|
||||
import crypto from "crypto";
|
||||
|
||||
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const { email } = req.body;
|
||||
const cleanEmail = email.toLowerCase();
|
||||
|
||||
if (!cleanEmail || !cleanEmail.includes("@")) {
|
||||
res.status(422).json({ message: "Invalid email" });
|
||||
return;
|
||||
}
|
||||
|
||||
const user = await prisma.user.findFirst({
|
||||
where: {
|
||||
email: cleanEmail,
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return res.status(400).json({ message: "No user found with this email." });
|
||||
}
|
||||
|
||||
const token = crypto.randomBytes(64).toString("hex");
|
||||
|
||||
const passwordResetToken = await prisma.passwordResetToken.create({
|
||||
data: {
|
||||
token,
|
||||
userId: user.id,
|
||||
},
|
||||
});
|
||||
|
||||
console.log(passwordResetToken);
|
||||
|
||||
// TODO: Send token to user via email
|
||||
|
||||
res.status(201).end();
|
||||
}
|
||||
|
||||
export default defaultHandler({
|
||||
POST: Promise.resolve({ default: defaultResponder(postHandler) }),
|
||||
});
|
||||
Reference in New Issue
Block a user