2021-10-13 11:35:25 +00:00
|
|
|
import debounce from "lodash/debounce";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { GetServerSidePropsContext } from "next";
|
2022-01-07 20:23:37 +00:00
|
|
|
import { getCsrfToken } from "next-auth/react";
|
2022-01-27 10:16:20 +00:00
|
|
|
import Link from "next/link";
|
2022-03-26 00:39:38 +00:00
|
|
|
import { useRouter } from "next/router";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { SyntheticEvent } from "react";
|
|
|
|
import React from "react";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2023-02-14 15:01:34 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { Button, EmailField } from "@calcom/ui";
|
2022-03-16 23:36:43 +00:00
|
|
|
|
2021-09-03 20:51:21 +00:00
|
|
|
import { getSession } from "@lib/auth";
|
2021-06-24 15:59:11 +00:00
|
|
|
|
2022-11-01 13:29:01 +00:00
|
|
|
import AuthContainer from "@components/ui/AuthContainer";
|
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("");
|
2022-03-26 00:39:38 +00:00
|
|
|
const router = useRouter();
|
2021-06-24 15:59:11 +00:00
|
|
|
|
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);
|
2022-01-07 20:23:37 +00:00
|
|
|
} else if ("resetLink" in json) {
|
2022-03-26 00:39:38 +00:00
|
|
|
router.push(json.resetLink);
|
2021-06-24 15:59:11 +00:00
|
|
|
} 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 (
|
2022-09-12 09:25:54 +00:00
|
|
|
<div className="space-y-6 text-sm leading-normal ">
|
|
|
|
<p className="">{t("password_reset_email", { email })}</p>
|
|
|
|
<p className="">{t("password_reset_leading")}</p>
|
2022-01-27 10:16:20 +00:00
|
|
|
{error && <p className="text-center text-red-600">{error.message}</p>}
|
2022-09-12 09:25:54 +00:00
|
|
|
<Button color="secondary" className="w-full justify-center" href="/auth/login">
|
|
|
|
{t("back_to_signin")}
|
|
|
|
</Button>
|
2021-06-24 15:59:11 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2022-01-27 10:16:20 +00:00
|
|
|
<AuthContainer
|
2022-09-12 09:25:54 +00:00
|
|
|
showLogo
|
|
|
|
title={!success ? t("forgot_password") : t("reset_link_sent")}
|
|
|
|
heading={!success ? t("forgot_password") : t("reset_link_sent")}
|
2022-01-27 10:16:20 +00:00
|
|
|
description={t("request_password_reset")}
|
|
|
|
footerText={
|
2022-09-12 09:25:54 +00:00
|
|
|
!success && (
|
|
|
|
<>
|
2023-01-12 16:57:43 +00:00
|
|
|
<Link href="/auth/login" className="font-medium text-gray-900">
|
2023-01-06 12:13:56 +00:00
|
|
|
{t("back_to_signin")}
|
2022-09-12 09:25:54 +00:00
|
|
|
</Link>
|
|
|
|
</>
|
|
|
|
)
|
2022-01-27 10:16:20 +00:00
|
|
|
}>
|
|
|
|
{success && <Success />}
|
|
|
|
{!success && (
|
|
|
|
<>
|
2022-09-12 09:25:54 +00:00
|
|
|
<div className="space-y-6">{error && <p className="text-red-600">{error.message}</p>}</div>
|
2022-01-27 10:16:20 +00:00
|
|
|
<form className="space-y-6" onSubmit={handleSubmit} action="#">
|
|
|
|
<input name="csrfToken" type="hidden" defaultValue={csrfToken} hidden />
|
|
|
|
<EmailField
|
|
|
|
onChange={handleChange}
|
|
|
|
id="email"
|
|
|
|
name="email"
|
|
|
|
label={t("email_address")}
|
|
|
|
placeholder="john.doe@example.com"
|
|
|
|
required
|
|
|
|
/>
|
|
|
|
<div className="space-y-2">
|
|
|
|
<Button
|
2022-02-09 00:05:13 +00:00
|
|
|
className="w-full justify-center"
|
2022-01-27 10:16:20 +00:00
|
|
|
type="submit"
|
|
|
|
disabled={loading}
|
|
|
|
aria-label={t("request_password_reset")}
|
|
|
|
loading={loading}>
|
|
|
|
{t("request_password_reset")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</AuthContainer>
|
2021-06-24 15:59:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
};
|