2021-10-13 11:35:25 +00:00
|
|
|
import debounce from "lodash/debounce";
|
2021-10-25 13:05:21 +00:00
|
|
|
import { GetServerSidePropsContext } from "next";
|
2021-09-22 19:52:38 +00:00
|
|
|
import { getCsrfToken } from "next-auth/client";
|
2021-10-25 13:05:21 +00:00
|
|
|
import React, { SyntheticEvent } from "react";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-09-03 20:51:21 +00:00
|
|
|
import { getSession } from "@lib/auth";
|
2021-10-14 14:24:21 +00:00
|
|
|
import { useLocale } from "@lib/hooks/useLocale";
|
2021-06-24 15:59:11 +00:00
|
|
|
|
2021-12-21 00:59:06 +00:00
|
|
|
import { EmailField } from "@components/form/fields";
|
2021-09-22 19:52:38 +00:00
|
|
|
import { HeadSeo } from "@components/seo/head-seo";
|
2021-12-17 00:03:32 +00:00
|
|
|
import Button from "@components/ui/Button";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-10-25 13:05:21 +00:00
|
|
|
export default function ForgotPassword({ csrfToken }: { csrfToken: string }) {
|
|
|
|
const { t, i18n } = useLocale();
|
2021-06-24 15:59:11 +00:00
|
|
|
const [loading, setLoading] = React.useState(false);
|
2021-10-25 13:05:21 +00:00
|
|
|
const [error, setError] = React.useState<{ message: string } | null>(null);
|
2021-06-24 15:59:11 +00:00
|
|
|
const [success, setSuccess] = React.useState(false);
|
|
|
|
const [email, setEmail] = React.useState("");
|
|
|
|
|
2021-10-25 13:05:21 +00:00
|
|
|
const handleChange = (e: SyntheticEvent) => {
|
|
|
|
const target = e.target as typeof e.target & { value: string };
|
|
|
|
setEmail(target.value);
|
2021-06-24 15:59:11 +00:00
|
|
|
};
|
|
|
|
|
2021-10-25 13:05:21 +00:00
|
|
|
const submitForgotPasswordRequest = async ({ email }: { email: string }) => {
|
2021-06-24 15:59:11 +00:00
|
|
|
try {
|
|
|
|
const res = await fetch("/api/auth/forgot-password", {
|
|
|
|
method: "POST",
|
2021-10-25 13:05:21 +00:00
|
|
|
body: JSON.stringify({ email: email, language: i18n.language }),
|
2021-06-24 15:59:11 +00:00
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const json = await res.json();
|
|
|
|
if (!res.ok) {
|
|
|
|
setError(json);
|
|
|
|
} else {
|
|
|
|
setSuccess(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return json;
|
|
|
|
} catch (reason) {
|
2021-10-14 14:24:21 +00:00
|
|
|
setError({ message: t("unexpected_error_try_again") });
|
2021-06-24 15:59:11 +00:00
|
|
|
} finally {
|
|
|
|
setLoading(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const debouncedHandleSubmitPasswordRequest = debounce(submitForgotPasswordRequest, 250);
|
|
|
|
|
2021-10-25 13:05:21 +00:00
|
|
|
const handleSubmit = async (e: SyntheticEvent) => {
|
2021-06-24 15:59:11 +00:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
if (!email) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setLoading(true);
|
|
|
|
setError(null);
|
|
|
|
setSuccess(false);
|
|
|
|
|
|
|
|
await debouncedHandleSubmitPasswordRequest({ email });
|
|
|
|
};
|
|
|
|
|
|
|
|
const Success = () => {
|
|
|
|
return (
|
|
|
|
<div className="space-y-6">
|
2021-12-14 10:39:32 +00:00
|
|
|
<h2 className="mt-6 text-3xl font-extrabold text-center text-gray-900">{t("done")}</h2>
|
2021-10-14 14:24:21 +00:00
|
|
|
<p>{t("check_email_reset_password")}</p>
|
2021-06-24 15:59:11 +00:00
|
|
|
{error && <p className="text-red-600">{error.message}</p>}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2021-12-14 10:39:32 +00:00
|
|
|
<div className="flex flex-col justify-center min-h-screen py-12 bg-gray-50 sm:px-6 lg:px-8">
|
2021-12-17 00:03:32 +00:00
|
|
|
<HeadSeo title={t("forgot_password")} description={t("request_password_reset")} />
|
2021-06-24 15:59:11 +00:00
|
|
|
<div className="mt-8 sm:mx-auto sm:w-full sm:max-w-md">
|
2021-12-17 00:03:32 +00:00
|
|
|
<div className="px-4 pt-3 pb-8 mx-2 space-y-6 bg-white rounded-lg shadow sm:px-10">
|
2021-06-24 15:59:11 +00:00
|
|
|
{success && <Success />}
|
|
|
|
{!success && (
|
|
|
|
<>
|
|
|
|
<div className="space-y-6">
|
2021-12-14 10:39:32 +00:00
|
|
|
<h2 className="mt-6 text-3xl font-extrabold text-center text-gray-900 font-cal">
|
2021-10-14 14:24:21 +00:00
|
|
|
{t("forgot_password")}
|
2021-09-22 21:23:19 +00:00
|
|
|
</h2>
|
2021-12-17 00:03:32 +00:00
|
|
|
<p className="text-sm text-gray-500">{t("reset_instructions")}</p>
|
2021-06-24 15:59:11 +00:00
|
|
|
{error && <p className="text-red-600">{error.message}</p>}
|
|
|
|
</div>
|
|
|
|
<form className="space-y-6" onSubmit={handleSubmit} action="#">
|
|
|
|
<input name="csrfToken" type="hidden" defaultValue={csrfToken} hidden />
|
|
|
|
|
2021-12-21 00:59:06 +00:00
|
|
|
<EmailField
|
2021-12-17 00:03:32 +00:00
|
|
|
onChange={handleChange}
|
|
|
|
id="email"
|
|
|
|
name="email"
|
|
|
|
label={t("email_address")}
|
|
|
|
placeholder="john.doe@example.com"
|
|
|
|
required
|
|
|
|
/>
|
|
|
|
<div className="space-y-2">
|
|
|
|
<Button
|
|
|
|
className="justify-center w-full"
|
2021-06-24 15:59:11 +00:00
|
|
|
type="submit"
|
|
|
|
disabled={loading}
|
2021-12-17 00:03:32 +00:00
|
|
|
aria-label={t("request_password_reset")}
|
|
|
|
loading={loading}>
|
2021-10-14 14:24:21 +00:00
|
|
|
{t("request_password_reset")}
|
2021-12-17 00:03:32 +00:00
|
|
|
</Button>
|
|
|
|
|
|
|
|
<Button
|
|
|
|
href="/auth/login"
|
|
|
|
color="minimal"
|
|
|
|
role="button"
|
|
|
|
aria-label={t("login_instead")}
|
|
|
|
className="justify-center w-full">
|
|
|
|
{t("login_instead")}
|
|
|
|
</Button>
|
2021-06-29 08:45:54 +00:00
|
|
|
</div>
|
2021-06-24 15:59:11 +00:00
|
|
|
</form>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:05:21 +00:00
|
|
|
ForgotPassword.getInitialProps = async (context: GetServerSidePropsContext) => {
|
2021-08-09 10:35:06 +00:00
|
|
|
const { req, res } = context;
|
|
|
|
const session = await getSession({ req });
|
|
|
|
|
|
|
|
if (session) {
|
|
|
|
res.writeHead(302, { Location: "/" });
|
|
|
|
res.end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-24 15:59:11 +00:00
|
|
|
return {
|
2021-08-09 10:35:06 +00:00
|
|
|
csrfToken: await getCsrfToken(context),
|
2021-06-24 15:59:11 +00:00
|
|
|
};
|
|
|
|
};
|