import { ResetPasswordRequest } from "@prisma/client"; import dayjs from "dayjs"; import debounce from "lodash/debounce"; import { GetServerSidePropsContext } from "next"; import { getCsrfToken } from "next-auth/react"; import Link from "next/link"; import React, { useMemo } from "react"; import { useLocale } from "@lib/hooks/useLocale"; import prisma from "@lib/prisma"; import { HeadSeo } from "@components/seo/head-seo"; type Props = { id: string; resetPasswordRequest: ResetPasswordRequest; csrfToken: string; }; export default function Page({ resetPasswordRequest, csrfToken }: Props) { const { t } = useLocale(); const [loading, setLoading] = React.useState(false); const [error, setError] = React.useState<{ message: string } | null>(null); const [success, setSuccess] = React.useState(false); const [password, setPassword] = React.useState(""); const submitChangePassword = async ({ password, requestId }: { password: string; requestId: string }) => { try { const res = await fetch("/api/auth/reset-password", { method: "POST", body: JSON.stringify({ requestId: requestId, password: password }), headers: { "Content-Type": "application/json", }, }); const json = await res.json(); if (!res.ok) { setError(json); } else { setSuccess(true); } return json; } catch (reason) { setError({ message: t("unexpected_error_try_again") }); } finally { setLoading(false); } }; const debouncedChangePassword = debounce(submitChangePassword, 250); const Success = () => { return ( <>

{t("success")}

{t("password_has_been_reset_login")}

); }; const Expired = () => { return ( <>

{t("whoops")}

{t("request_is_expired")}

{t("request_is_expired_instructions")}

); }; const isRequestExpired = useMemo(() => { const now = dayjs(); return dayjs(resetPasswordRequest.expires).isBefore(now); }, [resetPasswordRequest]); return (
{isRequestExpired && } {!isRequestExpired && !success && ( <>

{t("reset_password")}

{t("enter_new_password")}

{error &&

{error.message}

}
{ e.preventDefault(); if (!password) { return; } if (loading) { return; } setLoading(true); setError(null); setSuccess(false); await debouncedChangePassword({ password, requestId: resetPasswordRequest.id }); }} action="#">
{ setPassword(e.target.value); }} id="password" name="password" type="password" autoComplete="password" required className="focus:border-brand block w-full appearance-none rounded-md border border-gray-300 px-3 py-2 placeholder-gray-400 shadow-sm focus:outline-none focus:ring-black sm:text-sm" />
)} {!isRequestExpired && success && ( <> )}
); } export async function getServerSideProps(context: GetServerSidePropsContext) { const id = context.params?.id as string; try { const resetPasswordRequest = await prisma.resetPasswordRequest.findUnique({ rejectOnNotFound: true, where: { id, }, select: { id: true, expires: true, }, }); return { props: { resetPasswordRequest: { ...resetPasswordRequest, expires: resetPasswordRequest.expires.toString(), }, id, csrfToken: await getCsrfToken({ req: context.req }), }, }; } catch (reason) { return { notFound: true, }; } }