2021-09-21 09:29:20 +00:00
|
|
|
import React, { SyntheticEvent, useState } from "react";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2022-03-16 23:36:43 +00:00
|
|
|
import showToast from "@calcom/lib/notification";
|
|
|
|
import Button from "@calcom/ui/Button";
|
|
|
|
|
2021-09-21 09:29:20 +00:00
|
|
|
import { ErrorCode } from "@lib/auth";
|
2021-10-08 11:43:48 +00:00
|
|
|
import { useLocale } from "@lib/hooks/useLocale";
|
2021-11-29 06:37:31 +00:00
|
|
|
|
2021-10-12 13:11:33 +00:00
|
|
|
const ChangePasswordSection = () => {
|
2021-09-21 09:29:20 +00:00
|
|
|
const [oldPassword, setOldPassword] = useState("");
|
|
|
|
const [newPassword, setNewPassword] = useState("");
|
|
|
|
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
2021-10-12 13:11:33 +00:00
|
|
|
const { t } = useLocale();
|
2021-10-08 11:43:48 +00:00
|
|
|
|
|
|
|
const errorMessages: { [key: string]: string } = {
|
|
|
|
[ErrorCode.IncorrectPassword]: t("current_incorrect_password"),
|
|
|
|
[ErrorCode.NewPasswordMatchesOld]: t("new_password_matches_old_password"),
|
|
|
|
};
|
2021-09-21 09:29:20 +00:00
|
|
|
|
|
|
|
async function changePasswordHandler(e: SyntheticEvent) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
if (isSubmitting) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsSubmitting(true);
|
|
|
|
setErrorMessage(null);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await fetch("/api/auth/changepw", {
|
|
|
|
method: "PATCH",
|
|
|
|
body: JSON.stringify({ oldPassword, newPassword }),
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
setOldPassword("");
|
|
|
|
setNewPassword("");
|
2021-10-13 12:13:00 +00:00
|
|
|
showToast(t("password_has_been_changed"), "success");
|
2021-09-21 09:29:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const body = await response.json();
|
2021-10-08 11:43:48 +00:00
|
|
|
setErrorMessage(errorMessages[body.error] || `${t("something_went_wrong")}${t("please_try_again")}`);
|
2021-09-21 09:29:20 +00:00
|
|
|
} catch (err) {
|
2021-10-08 11:43:48 +00:00
|
|
|
console.error(t("error_changing_password"), err);
|
|
|
|
setErrorMessage(`${t("something_went_wrong")}${t("please_try_again")}`);
|
2021-09-21 09:29:20 +00:00
|
|
|
} finally {
|
|
|
|
setIsSubmitting(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className="mt-6">
|
2022-02-09 00:05:13 +00:00
|
|
|
<h2 className="font-cal text-lg font-medium leading-6 text-gray-900">{t("change_password")}</h2>
|
2021-09-21 09:29:20 +00:00
|
|
|
</div>
|
|
|
|
<form className="divide-y divide-gray-200 lg:col-span-9" onSubmit={changePasswordHandler}>
|
|
|
|
<div className="py-6 lg:pb-8">
|
|
|
|
<div className="flex">
|
2022-02-01 22:17:37 +00:00
|
|
|
<div className="w-1/2 ltr:mr-2 rtl:ml-2">
|
2021-09-21 09:29:20 +00:00
|
|
|
<label htmlFor="current_password" className="block text-sm font-medium text-gray-700">
|
2021-10-08 11:43:48 +00:00
|
|
|
{t("current_password")}
|
2021-09-21 09:29:20 +00:00
|
|
|
</label>
|
|
|
|
<div className="mt-1">
|
|
|
|
<input
|
|
|
|
type="password"
|
|
|
|
value={oldPassword}
|
|
|
|
onInput={(e) => setOldPassword(e.currentTarget.value)}
|
|
|
|
name="current_password"
|
|
|
|
id="current_password"
|
|
|
|
required
|
2022-04-14 14:58:23 +00:00
|
|
|
className="block w-full rounded-sm border-gray-300 shadow-sm sm:text-sm"
|
2021-10-08 11:43:48 +00:00
|
|
|
placeholder={t("your_old_password")}
|
2021-09-21 09:29:20 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-02-09 00:05:13 +00:00
|
|
|
<div className="ml-2 w-1/2">
|
2021-09-21 09:29:20 +00:00
|
|
|
<label htmlFor="new_password" className="block text-sm font-medium text-gray-700">
|
2021-10-08 11:43:48 +00:00
|
|
|
{t("new_password")}
|
2021-09-21 09:29:20 +00:00
|
|
|
</label>
|
|
|
|
<div className="mt-1">
|
|
|
|
<input
|
|
|
|
type="password"
|
|
|
|
name="new_password"
|
|
|
|
id="new_password"
|
|
|
|
value={newPassword}
|
|
|
|
required
|
|
|
|
onInput={(e) => setNewPassword(e.currentTarget.value)}
|
2022-04-14 14:58:23 +00:00
|
|
|
className="block w-full rounded-sm border-gray-300 shadow-sm sm:text-sm"
|
2021-10-08 11:43:48 +00:00
|
|
|
placeholder={t("super_secure_new_password")}
|
2021-09-21 09:29:20 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{errorMessage && <p className="mt-1 text-sm text-red-700">{errorMessage}</p>}
|
2022-02-01 22:17:37 +00:00
|
|
|
<div className="flex justify-end py-8">
|
2021-11-29 06:37:31 +00:00
|
|
|
<Button type="submit">{t("save")}</Button>
|
2021-09-21 09:29:20 +00:00
|
|
|
</div>
|
|
|
|
<hr className="mt-4" />
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ChangePasswordSection;
|