Fixes from code review
This commit is contained in:
@@ -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,9 +70,8 @@ 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)}>
|
||||
{!resetSuccessful && (
|
||||
<form className="mt-8 space-y-6" onSubmit={handleSubmit(onSubmit)}>
|
||||
<div className="-space-y-px rounded-md shadow-sm">
|
||||
<div>
|
||||
<label htmlFor="email-address" className="sr-only">
|
||||
@@ -102,7 +99,6 @@ export default function ForgotPassword() {
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</FormProvider>
|
||||
)}
|
||||
<div>
|
||||
<Link href="/login">
|
||||
|
||||
@@ -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,8 +78,7 @@ export default function ResetPassword() {
|
||||
{resetSuccessful ? "Your password has been reset." : "Please chose your new password"}
|
||||
</p>
|
||||
</div>
|
||||
{resetSuccessful ? null : (
|
||||
<FormProvider {...methods}>
|
||||
{!resetSuccessful && (
|
||||
<form className="mt-8 space-y-6" onSubmit={handleSubmit(onSubmit)}>
|
||||
<div className="-space-y-px rounded-md shadow-sm">
|
||||
<div>
|
||||
@@ -127,7 +125,6 @@ export default function ResetPassword() {
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</FormProvider>
|
||||
)}
|
||||
<div>
|
||||
<Link href="/login">
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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: {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user