From e3bc41934c69a904c85b054851def2f30e465046 Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Fri, 9 Jun 2023 03:55:30 +0000 Subject: [PATCH] Fixes from code review --- apps/web/components/forgot-password.tsx | 60 +++++++------- apps/web/components/reset-password.tsx | 99 +++++++++++------------ apps/web/pages/api/auth/reset-password.ts | 2 +- apps/web/pages/forgot-password.tsx | 6 +- packages/lib/server/getUserFromToken.ts | 17 ++-- 5 files changed, 86 insertions(+), 98 deletions(-) diff --git a/apps/web/components/forgot-password.tsx b/apps/web/components/forgot-password.tsx index cafdd067e..8235a80d9 100644 --- a/apps/web/components/forgot-password.tsx +++ b/apps/web/components/forgot-password.tsx @@ -11,9 +11,7 @@ interface ForgotPasswordForm { } export default function ForgotPassword() { - const methods = useForm(); - const { register, formState, resetField } = methods; - + const { register, formState, resetField, handleSubmit } = useForm(); const [resetSuccessful, setResetSuccessful] = useState(false); const onSubmit = async (values: ForgotPasswordForm) => { @@ -72,37 +70,35 @@ export default function ForgotPassword() { : "No worries, we'll send you reset instructions."}

- {resetSuccessful ? null : ( - -
-
-
- - -
-
- + {!resetSuccessful && ( + +
- + +
- - +
+ +
+ +
+ )}
diff --git a/apps/web/components/reset-password.tsx b/apps/web/components/reset-password.tsx index 03dc6df44..954a6b0da 100644 --- a/apps/web/components/reset-password.tsx +++ b/apps/web/components/reset-password.tsx @@ -25,14 +25,13 @@ export default function ResetPassword() { const router = useRouter(); const { token } = router.query; - const methods = useForm({ - resolver: zodResolver(schema), - }); const { register, formState: { errors, isSubmitting }, handleSubmit, - } = methods; + } = useForm({ + resolver: zodResolver(schema), + }); const [resetSuccessful, setResetSuccessful] = useState(false); @@ -79,55 +78,53 @@ export default function ResetPassword() { {resetSuccessful ? "Your password has been reset." : "Please chose your new password"}

- {resetSuccessful ? null : ( - -
-
-
- - -
-
- - -
-
- - {errors && ( - {errors.confirmPassword?.message} - )} - + {!resetSuccessful && ( + +
- + +
- - +
+ + +
+
+ + {errors && ( + {errors.confirmPassword?.message} + )} + +
+ +
+ )}
diff --git a/apps/web/pages/api/auth/reset-password.ts b/apps/web/pages/api/auth/reset-password.ts index bc42e2339..78a81b7d4 100644 --- a/apps/web/pages/api/auth/reset-password.ts +++ b/apps/web/pages/api/auth/reset-password.ts @@ -8,7 +8,7 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) { const { token, password } = req.body; if (!token) { - res.status(422).json({ message: "Invalid token" }); + res.status(400).json({ message: "Invalid token" }); return; } diff --git a/apps/web/pages/forgot-password.tsx b/apps/web/pages/forgot-password.tsx index b15bec499..4591921a4 100644 --- a/apps/web/pages/forgot-password.tsx +++ b/apps/web/pages/forgot-password.tsx @@ -1,3 +1,4 @@ +import { GetServerSideProps, GetServerSidePropsContext } from "next"; import Head from "next/head"; import { getUserFromToken } from "@documenso/lib/server"; import ForgotPassword from "../components/forgot-password"; @@ -13,8 +14,9 @@ export default function ForgotPasswordPage() { ); } -export async function getServerSideProps(context: any) { - const user = await getUserFromToken(context.req, context.res); +export async function getServerSideProps({ req }: GetServerSidePropsContext) { + const user = await getUserFromToken(req); + if (user) return { redirect: { diff --git a/packages/lib/server/getUserFromToken.ts b/packages/lib/server/getUserFromToken.ts index 0cb8b72bb..f38c4abd1 100644 --- a/packages/lib/server/getUserFromToken.ts +++ b/packages/lib/server/getUserFromToken.ts @@ -1,23 +1,17 @@ -import { NextApiRequest, NextApiResponse } from "next"; +import { GetServerSidePropsContext, NextApiRequest, NextApiResponse } from "next"; +import { NextRequest } from "next/server"; import prisma from "@documenso/prisma"; import { User as PrismaUser } from "@prisma/client"; import { getToken } from "next-auth/jwt"; -import { signOut } from "next-auth/react"; export async function getUserFromToken( - req: NextApiRequest, - res: NextApiResponse + req: GetServerSidePropsContext["req"] | NextRequest | NextApiRequest, + res?: NextApiResponse // TODO: Remove this optional parameter ): Promise { const token = await getToken({ req }); const tokenEmail = token?.email?.toString(); - if (!token) { - if (res.status) res.status(401).send("No session token found for request."); - return null; - } - - if (!tokenEmail) { - res.status(400).send("No email found in session token."); + if (!token || !tokenEmail) { return null; } @@ -26,7 +20,6 @@ export async function getUserFromToken( }); if (!user) { - if (res && res.status) res.status(401).end(); return null; }