2024-06-09 09:02:45 -05:00
|
|
|
import Button from "@/components/ui/Button";
|
2024-05-18 22:57:00 -05:00
|
|
|
import TextInput from "@/components/TextInput";
|
|
|
|
import CenteredForm from "@/layouts/CenteredForm";
|
|
|
|
import Link from "next/link";
|
2024-05-20 18:23:11 -05:00
|
|
|
import { useRouter } from "next/router";
|
2024-05-18 22:57:00 -05:00
|
|
|
import { FormEvent, useState } from "react";
|
|
|
|
import { toast } from "react-hot-toast";
|
2024-06-04 15:59:49 -05:00
|
|
|
import getServerSideProps from "@/lib/client/getServerSideProps";
|
|
|
|
import { useTranslation } from "next-i18next";
|
2024-05-18 22:57:00 -05:00
|
|
|
|
|
|
|
interface FormData {
|
|
|
|
password: string;
|
2024-05-20 18:23:11 -05:00
|
|
|
token: string;
|
2024-05-18 22:57:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function ResetPassword() {
|
2024-06-04 15:59:49 -05:00
|
|
|
const { t } = useTranslation();
|
2024-05-18 22:57:00 -05:00
|
|
|
const [submitLoader, setSubmitLoader] = useState(false);
|
2024-05-20 18:23:11 -05:00
|
|
|
const router = useRouter();
|
|
|
|
|
2024-05-18 22:57:00 -05:00
|
|
|
const [form, setForm] = useState<FormData>({
|
|
|
|
password: "",
|
2024-05-20 18:23:11 -05:00
|
|
|
token: router.query.token as string,
|
2024-05-18 22:57:00 -05:00
|
|
|
});
|
|
|
|
|
2024-05-20 18:23:11 -05:00
|
|
|
const [requestSent, setRequestSent] = useState(false);
|
2024-05-18 22:57:00 -05:00
|
|
|
|
2024-05-20 18:23:11 -05:00
|
|
|
async function submit(event: FormEvent<HTMLFormElement>) {
|
2024-05-18 22:57:00 -05:00
|
|
|
event.preventDefault();
|
|
|
|
|
2024-05-20 18:23:11 -05:00
|
|
|
if (
|
|
|
|
form.password !== "" &&
|
|
|
|
form.token !== "" &&
|
|
|
|
!requestSent &&
|
|
|
|
!submitLoader
|
|
|
|
) {
|
2024-05-18 22:57:00 -05:00
|
|
|
setSubmitLoader(true);
|
|
|
|
|
2024-06-04 15:59:49 -05:00
|
|
|
const load = toast.loading(t("sending_password_recovery_link"));
|
2024-05-18 22:57:00 -05:00
|
|
|
|
2024-05-20 18:23:11 -05:00
|
|
|
const response = await fetch("/api/v1/auth/reset-password", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(form),
|
|
|
|
});
|
|
|
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
2024-06-04 15:59:49 -05:00
|
|
|
toast.dismiss(load);
|
2024-05-20 18:23:11 -05:00
|
|
|
if (response.ok) {
|
|
|
|
toast.success(data.response);
|
|
|
|
setRequestSent(true);
|
|
|
|
} else {
|
|
|
|
toast.error(data.response);
|
|
|
|
}
|
2024-05-18 22:57:00 -05:00
|
|
|
|
|
|
|
setSubmitLoader(false);
|
|
|
|
} else {
|
2024-06-04 15:59:49 -05:00
|
|
|
toast.error(t("please_fill_all_fields"));
|
2024-05-18 22:57:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<CenteredForm>
|
2024-05-20 18:23:11 -05:00
|
|
|
<form onSubmit={submit}>
|
2024-05-18 22:57:00 -05:00
|
|
|
<div className="p-4 mx-auto flex flex-col gap-3 justify-between max-w-[30rem] min-w-80 w-full bg-base-200 rounded-2xl shadow-md border border-neutral-content">
|
|
|
|
<p className="text-3xl text-center font-extralight">
|
2024-06-04 15:59:49 -05:00
|
|
|
{requestSent ? t("password_updated") : t("reset_password")}
|
2024-05-18 22:57:00 -05:00
|
|
|
</p>
|
|
|
|
|
|
|
|
<div className="divider my-0"></div>
|
|
|
|
|
2024-05-20 18:23:11 -05:00
|
|
|
{!requestSent ? (
|
2024-05-18 22:57:00 -05:00
|
|
|
<>
|
2024-06-04 15:59:49 -05:00
|
|
|
<p>{t("enter_email_for_new_password")}</p>
|
2024-05-18 22:57:00 -05:00
|
|
|
<div>
|
2024-06-04 15:59:49 -05:00
|
|
|
<p className="text-sm w-fit font-semibold mb-1">
|
|
|
|
{t("new_password")}
|
2024-05-18 22:57:00 -05:00
|
|
|
</p>
|
|
|
|
<TextInput
|
|
|
|
autoFocus
|
|
|
|
type="password"
|
2024-05-20 18:23:11 -05:00
|
|
|
placeholder="••••••••••••••"
|
2024-05-18 22:57:00 -05:00
|
|
|
value={form.password}
|
|
|
|
className="bg-base-100"
|
|
|
|
onChange={(e) =>
|
|
|
|
setForm({ ...form, password: e.target.value })
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</div>
|
2024-06-09 09:02:45 -05:00
|
|
|
<Button
|
2024-05-18 22:57:00 -05:00
|
|
|
type="submit"
|
2024-05-22 19:56:56 -05:00
|
|
|
intent="accent"
|
|
|
|
className="mt-2"
|
|
|
|
size="full"
|
2024-05-18 22:57:00 -05:00
|
|
|
loading={submitLoader}
|
2024-05-22 19:56:56 -05:00
|
|
|
>
|
2024-06-04 15:59:49 -05:00
|
|
|
{t("update_password")}
|
2024-06-09 09:02:45 -05:00
|
|
|
</Button>
|
2024-05-18 22:57:00 -05:00
|
|
|
</>
|
|
|
|
) : (
|
2024-05-20 18:23:11 -05:00
|
|
|
<>
|
2024-06-04 15:59:49 -05:00
|
|
|
<p>{t("password_successfully_updated")}</p>
|
2024-05-20 18:23:11 -05:00
|
|
|
<div className="mx-auto w-fit mt-3">
|
|
|
|
<Link className="font-semibold" href="/login">
|
2024-06-04 15:59:49 -05:00
|
|
|
{t("back_to_login")}
|
2024-05-20 18:23:11 -05:00
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
2024-05-18 22:57:00 -05:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</CenteredForm>
|
|
|
|
);
|
|
|
|
}
|
2024-06-04 15:59:49 -05:00
|
|
|
|
|
|
|
export { getServerSideProps };
|