import { HeadSeo } from "@components/seo/head-seo"; import Link from "next/link"; import React from "react"; import { getCsrfToken } from "next-auth/client"; import debounce from "lodash.debounce"; import { getSession } from "@lib/auth"; 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), }; };