import type { ResetPasswordRequest } from "@prisma/client"; import type { GetServerSidePropsContext } from "next"; import { getCsrfToken } from "next-auth/react"; import { serverSideTranslations } from "next-i18next/serverSideTranslations"; import Link from "next/link"; import type { CSSProperties } from "react"; import React, { useMemo } from "react"; import { useForm } from "react-hook-form"; import dayjs from "@calcom/dayjs"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import prisma from "@calcom/prisma"; import { Button, PasswordField, Form } from "@calcom/ui"; import PageWrapper from "@components/PageWrapper"; import AuthContainer from "@components/ui/AuthContainer"; type Props = { id: string; resetPasswordRequest: ResetPasswordRequest; csrfToken: string; }; export default function Page({ resetPasswordRequest, csrfToken }: Props) { const { t } = useLocale(); const formMethods = useForm<{ new_password: string }>(); const success = formMethods.formState.isSubmitSuccessful; const loading = formMethods.formState.isSubmitting; const submitChangePassword = async ({ password, requestId }: { password: string; requestId: string }) => { const res = await fetch("/api/auth/reset-password", { method: "POST", body: JSON.stringify({ requestId, password }), headers: { "Content-Type": "application/json", }, }); const json = await res.json(); if (!res.ok) return formMethods.setError("new_password", { type: "server", message: json.message }); }; const Success = () => { return ( <>

{t("password_updated")}

); }; 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 && ( <>
{ await submitChangePassword({ password: values.new_password, requestId: resetPasswordRequest.id, }); }}>
)} {!isRequestExpired && success && ( <> )}
); } Page.isThemeSupported = false; Page.PageWrapper = PageWrapper; export async function getServerSideProps(context: GetServerSidePropsContext) { const id = context.params?.id as string; try { const resetPasswordRequest = await prisma.resetPasswordRequest.findUniqueOrThrow({ where: { id, }, select: { id: true, expires: true, }, }); return { props: { resetPasswordRequest: { ...resetPasswordRequest, expires: resetPasswordRequest.expires.toString(), }, id, csrfToken: await getCsrfToken({ req: context.req }), ...(await serverSideTranslations(context.locale || "en", ["common"])), }, }; } catch (reason) { return { notFound: true, }; } }