Fixes from code review

This commit is contained in:
Ephraim Atta-Duncan
2023-06-09 03:55:30 +00:00
parent 13a840ff78
commit e3bc41934c
5 changed files with 86 additions and 98 deletions

View File

@ -11,9 +11,7 @@ interface ForgotPasswordForm {
}
export default function ForgotPassword() {
const methods = useForm<ForgotPasswordForm>();
const { register, formState, resetField } = methods;
const { register, formState, resetField, handleSubmit } = useForm<ForgotPasswordForm>();
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."}
</p>
</div>
{resetSuccessful ? null : (
<FormProvider {...methods}>
<form className="mt-8 space-y-6" onSubmit={methods.handleSubmit(onSubmit)}>
<div className="-space-y-px rounded-md shadow-sm">
<div>
<label htmlFor="email-address" className="sr-only">
Email
</label>
<input
{...register("email")}
id="email-address"
name="email"
type="email"
autoComplete="email"
required
className="focus:border-neon focus:ring-neon relative block w-full appearance-none rounded-md border border-gray-300 px-3 py-2 text-gray-900 placeholder-gray-500 focus:z-10 focus:outline-none sm:text-sm"
placeholder="Email"
/>
</div>
</div>
{!resetSuccessful && (
<form className="mt-8 space-y-6" onSubmit={handleSubmit(onSubmit)}>
<div className="-space-y-px rounded-md shadow-sm">
<div>
<Button
type="submit"
disabled={formState.isSubmitting}
className="group relative flex w-full">
Reset password
</Button>
<label htmlFor="email-address" className="sr-only">
Email
</label>
<input
{...register("email")}
id="email-address"
name="email"
type="email"
autoComplete="email"
required
className="focus:border-neon focus:ring-neon relative block w-full appearance-none rounded-md border border-gray-300 px-3 py-2 text-gray-900 placeholder-gray-500 focus:z-10 focus:outline-none sm:text-sm"
placeholder="Email"
/>
</div>
</form>
</FormProvider>
</div>
<div>
<Button
type="submit"
disabled={formState.isSubmitting}
className="group relative flex w-full">
Reset password
</Button>
</div>
</form>
)}
<div>
<Link href="/login">

View File

@ -25,14 +25,13 @@ export default function ResetPassword() {
const router = useRouter();
const { token } = router.query;
const methods = useForm<ResetPasswordForm>({
resolver: zodResolver(schema),
});
const {
register,
formState: { errors, isSubmitting },
handleSubmit,
} = methods;
} = useForm<ResetPasswordForm>({
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"}
</p>
</div>
{resetSuccessful ? null : (
<FormProvider {...methods}>
<form className="mt-8 space-y-6" onSubmit={handleSubmit(onSubmit)}>
<div className="-space-y-px rounded-md shadow-sm">
<div>
<label htmlFor="password" className="sr-only">
Password
</label>
<input
{...register("password", { required: "Password is required" })}
id="password"
name="password"
type="password"
autoComplete="current-password"
required
className="focus:border-neon focus:ring-neon relative block w-full appearance-none rounded-none rounded-t-md border border-gray-300 px-3 py-2 text-gray-900 placeholder-gray-500 focus:z-10 focus:outline-none sm:text-sm"
placeholder="New password"
/>
</div>
<div>
<label htmlFor="confirmPassword" className="sr-only">
Password
</label>
<input
{...register("confirmPassword")}
id="confirmPassword"
name="confirmPassword"
type="password"
required
className="focus:border-neon focus:ring-neon relative block w-full appearance-none rounded-none rounded-b-md border border-gray-300 px-3 py-2 text-gray-900 placeholder-gray-500 focus:z-10 focus:outline-none sm:text-sm"
placeholder="Confirm new password"
/>
</div>
</div>
{errors && (
<span className="text-xs text-red-500">{errors.confirmPassword?.message}</span>
)}
{!resetSuccessful && (
<form className="mt-8 space-y-6" onSubmit={handleSubmit(onSubmit)}>
<div className="-space-y-px rounded-md shadow-sm">
<div>
<Button
type="submit"
disabled={isSubmitting}
className="group relative flex w-full">
Reset password
</Button>
<label htmlFor="password" className="sr-only">
Password
</label>
<input
{...register("password", { required: "Password is required" })}
id="password"
name="password"
type="password"
autoComplete="current-password"
required
className="focus:border-neon focus:ring-neon relative block w-full appearance-none rounded-none rounded-t-md border border-gray-300 px-3 py-2 text-gray-900 placeholder-gray-500 focus:z-10 focus:outline-none sm:text-sm"
placeholder="New password"
/>
</div>
</form>
</FormProvider>
<div>
<label htmlFor="confirmPassword" className="sr-only">
Password
</label>
<input
{...register("confirmPassword")}
id="confirmPassword"
name="confirmPassword"
type="password"
required
className="focus:border-neon focus:ring-neon relative block w-full appearance-none rounded-none rounded-b-md border border-gray-300 px-3 py-2 text-gray-900 placeholder-gray-500 focus:z-10 focus:outline-none sm:text-sm"
placeholder="Confirm new password"
/>
</div>
</div>
{errors && (
<span className="text-xs text-red-500">{errors.confirmPassword?.message}</span>
)}
<div>
<Button
type="submit"
disabled={isSubmitting}
className="group relative flex w-full">
Reset password
</Button>
</div>
</form>
)}
<div>
<Link href="/login">

View File

@ -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;
}

View File

@ -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: {

View File

@ -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<PrismaUser | null> {
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;
}