el.xwx.moe/pages/auth/reset-password.tsx

120 lines
3.3 KiB
TypeScript
Raw Normal View History

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";
import { useRouter } from "next/router";
2024-05-18 22:57:00 -05:00
import { FormEvent, useState } from "react";
import { toast } from "react-hot-toast";
import getServerSideProps from "@/lib/client/getServerSideProps";
import { useTranslation } from "next-i18next";
2024-05-18 22:57:00 -05:00
interface FormData {
password: string;
token: string;
2024-05-18 22:57:00 -05:00
}
export default function ResetPassword() {
const { t } = useTranslation();
2024-05-18 22:57:00 -05:00
const [submitLoader, setSubmitLoader] = useState(false);
const router = useRouter();
2024-05-18 22:57:00 -05:00
const [form, setForm] = useState<FormData>({
password: "",
token: router.query.token as string,
2024-05-18 22:57:00 -05:00
});
const [requestSent, setRequestSent] = useState(false);
2024-05-18 22:57:00 -05:00
async function submit(event: FormEvent<HTMLFormElement>) {
2024-05-18 22:57:00 -05:00
event.preventDefault();
if (
form.password !== "" &&
form.token !== "" &&
!requestSent &&
!submitLoader
) {
2024-05-18 22:57:00 -05:00
setSubmitLoader(true);
const load = toast.loading(t("sending_password_recovery_link"));
2024-05-18 22:57:00 -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();
toast.dismiss(load);
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 {
toast.error(t("please_fill_all_fields"));
2024-05-18 22:57:00 -05:00
}
}
return (
<CenteredForm>
<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">
{requestSent ? t("password_updated") : t("reset_password")}
2024-05-18 22:57:00 -05:00
</p>
<div className="divider my-0"></div>
{!requestSent ? (
2024-05-18 22:57:00 -05:00
<>
<p>{t("enter_email_for_new_password")}</p>
2024-05-18 22:57:00 -05:00
<div>
<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"
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>
<Button
2024-05-18 22:57:00 -05:00
type="submit"
intent="accent"
className="mt-2"
size="full"
2024-05-18 22:57:00 -05:00
loading={submitLoader}
>
{t("update_password")}
</Button>
2024-05-18 22:57:00 -05:00
</>
) : (
<>
<p>{t("password_successfully_updated")}</p>
<div className="mx-auto w-fit mt-3">
<Link className="font-semibold" href="/login">
{t("back_to_login")}
</Link>
</div>
</>
)}
2024-05-18 22:57:00 -05:00
</div>
</form>
</CenteredForm>
);
}
export { getServerSideProps };