import debounce from "lodash.debounce"; import { getCsrfToken } from "next-auth/client"; import Link from "next/link"; import React from "react"; import { getSession } from "@lib/auth"; import { HeadSeo } from "@components/seo/head-seo"; export default function ForgotPassword({ csrfToken }) { const [loading, setLoading] = React.useState(false); const [error, setError] = React.useState(null); const [success, setSuccess] = React.useState(false); const [email, setEmail] = React.useState(""); const handleChange = (e) => { setEmail(e.target.value); }; const submitForgotPasswordRequest = async ({ email }) => { try { const res = await fetch("/api/auth/forgot-password", { method: "POST", body: JSON.stringify({ email: email }), 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 debouncedHandleSubmitPasswordRequest = debounce(submitForgotPasswordRequest, 250); const handleSubmit = async (e) => { e.preventDefault(); if (!email) { return; } if (loading) { return; } setLoading(true); setError(null); setSuccess(false); await debouncedHandleSubmitPasswordRequest({ email }); }; const Success = () => { return (

Done

Check your email. We sent you a link to reset your password.

{error &&

{error.message}

}
); }; return (
{success && } {!success && ( <>

Forgot Password

Enter the email address associated with your account and we will send you a link to reset your password.

{error &&

{error.message}

}
)}
); } ForgotPassword.getInitialProps = async (context) => { const { req, res } = context; const session = await getSession({ req }); if (session) { res.writeHead(302, { Location: "/" }); res.end(); return; } return { csrfToken: await getCsrfToken(context), }; };