2023-12-07 11:29:45 -06:00
|
|
|
|
import AccentSubmitButton from "@/components/AccentSubmitButton";
|
2023-08-17 15:05:44 -05:00
|
|
|
|
import TextInput from "@/components/TextInput";
|
2023-08-01 15:20:05 -05:00
|
|
|
|
import CenteredForm from "@/layouts/CenteredForm";
|
2023-07-12 13:26:34 -05:00
|
|
|
|
import Link from "next/link";
|
2023-10-23 00:20:08 -05:00
|
|
|
|
import { FormEvent, useState } from "react";
|
2023-07-12 13:26:34 -05:00
|
|
|
|
import { toast } from "react-hot-toast";
|
|
|
|
|
|
|
|
|
|
interface FormData {
|
|
|
|
|
email: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function Forgot() {
|
|
|
|
|
const [submitLoader, setSubmitLoader] = useState(false);
|
|
|
|
|
|
|
|
|
|
const [form, setForm] = useState<FormData>({
|
|
|
|
|
email: "",
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-18 22:57:00 -05:00
|
|
|
|
const [isEmailSent, setIsEmailSent] = useState(false);
|
|
|
|
|
|
|
|
|
|
async function submitRequest() {
|
|
|
|
|
const response = await fetch("/api/v1/auth/forgot-password", {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify(form),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
|
toast.success(data.response);
|
|
|
|
|
setIsEmailSent(true);
|
|
|
|
|
} else {
|
|
|
|
|
toast.error(data.response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-23 00:20:08 -05:00
|
|
|
|
async function sendConfirmation(event: FormEvent<HTMLFormElement>) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
2023-07-12 13:26:34 -05:00
|
|
|
|
if (form.email !== "") {
|
|
|
|
|
setSubmitLoader(true);
|
|
|
|
|
|
2024-05-18 22:57:00 -05:00
|
|
|
|
const load = toast.loading("Sending password recovery link...");
|
2023-07-12 13:26:34 -05:00
|
|
|
|
|
2024-05-18 22:57:00 -05:00
|
|
|
|
await submitRequest();
|
2023-07-12 13:26:34 -05:00
|
|
|
|
|
|
|
|
|
toast.dismiss(load);
|
|
|
|
|
|
|
|
|
|
setSubmitLoader(false);
|
|
|
|
|
} else {
|
|
|
|
|
toast.error("Please fill out all the fields.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2023-08-01 15:20:05 -05:00
|
|
|
|
<CenteredForm>
|
2023-10-23 00:20:08 -05:00
|
|
|
|
<form onSubmit={sendConfirmation}>
|
2023-11-26 04:17:08 -06: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">
|
2023-11-24 07:39:55 -06:00
|
|
|
|
<p className="text-3xl text-center font-extralight">
|
2024-05-18 22:57:00 -05:00
|
|
|
|
{isEmailSent ? "Email Sent!" : "Forgot Password?"}
|
2023-08-14 22:25:25 -05:00
|
|
|
|
</p>
|
2023-10-24 14:57:37 -05:00
|
|
|
|
|
2023-12-01 11:01:56 -06:00
|
|
|
|
<div className="divider my-0"></div>
|
2023-10-24 14:57:37 -05:00
|
|
|
|
|
2024-05-18 22:57:00 -05:00
|
|
|
|
{!isEmailSent ? (
|
|
|
|
|
<>
|
|
|
|
|
<div>
|
|
|
|
|
<p>
|
|
|
|
|
Enter your email so we can send you a link to create a new
|
|
|
|
|
password.
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
<div>
|
|
|
|
|
<p className="text-sm w-fit font-semibold mb-1">Email</p>
|
|
|
|
|
|
|
|
|
|
<TextInput
|
|
|
|
|
autoFocus
|
|
|
|
|
type="email"
|
|
|
|
|
placeholder="johnny@example.com"
|
|
|
|
|
value={form.email}
|
|
|
|
|
className="bg-base-100"
|
|
|
|
|
onChange={(e) => setForm({ ...form, email: e.target.value })}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<AccentSubmitButton
|
|
|
|
|
type="submit"
|
|
|
|
|
label="Send Login Link"
|
|
|
|
|
className="mt-2 w-full"
|
|
|
|
|
loading={submitLoader}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<p className="text-center">
|
|
|
|
|
Check your email for a link to reset your password. If it doesn’t
|
|
|
|
|
appear within a few minutes, check your spam folder.
|
2023-10-23 00:20:08 -05:00
|
|
|
|
</p>
|
2024-05-18 22:57:00 -05:00
|
|
|
|
)}
|
2023-07-12 13:26:34 -05:00
|
|
|
|
|
2023-10-23 00:20:08 -05:00
|
|
|
|
<div className="flex items-baseline gap-1 justify-center">
|
2023-11-24 07:39:55 -06:00
|
|
|
|
<Link href={"/login"} className="block font-bold">
|
2023-10-23 00:20:08 -05:00
|
|
|
|
Go back
|
|
|
|
|
</Link>
|
|
|
|
|
</div>
|
2023-07-13 19:42:04 -05:00
|
|
|
|
</div>
|
2023-10-23 00:20:08 -05:00
|
|
|
|
</form>
|
2023-08-01 15:20:05 -05:00
|
|
|
|
</CenteredForm>
|
2023-07-12 13:26:34 -05:00
|
|
|
|
);
|
|
|
|
|
}
|