import { getCsrfToken } from "next-auth/client"; import prisma from "@lib/prisma"; import { HeadSeo } from "@components/seo/head-seo"; import React, { useMemo } from "react"; import debounce from "lodash.debounce"; import dayjs from "dayjs"; import { ResetPasswordRequest } from "@prisma/client"; import Link from "next/link"; import { GetServerSidePropsContext } from "next"; type Props = { id: string; resetPasswordRequest: ResetPasswordRequest; csrfToken: string; }; export default function Page({ resetPasswordRequest, csrfToken }: Props) { const [loading, setLoading] = React.useState(false); const [error, setError] = React.useState(null); const [success, setSuccess] = React.useState(false); const [password, setPassword] = React.useState(""); const handleChange = (e) => { setPassword(e.target.value); }; const submitChangePassword = async ({ password, requestId }) => { 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: "An unexpected error occurred. Try again." }); } finally { setLoading(false); } }; const debouncedChangePassword = debounce(submitChangePassword, 250); const handleSubmit = async (e) => { e.preventDefault(); if (!password) { return; } if (loading) { return; } setLoading(true); setError(null); setSuccess(false); await debouncedChangePassword({ password, requestId: resetPasswordRequest.id }); }; const Success = () => { return ( <>
Your password has been reset. You can now login with your newly created password.
That request is expired. You can back and enter the email associated with your account and we will you another link to reset your password.
Enter the new password you'd like for your account.
{error &&{error.message}
}