Implemented the reset password mechanism

This commit is contained in:
Tiago Ribeiro
2023-08-31 21:48:02 +01:00
parent f2323b35b8
commit 41d6303403
14 changed files with 180 additions and 13 deletions

View File

@@ -12,6 +12,8 @@ import Link from "next/link";
import Input from "@/components/Low/Input";
import clsx from "clsx";
const EMAIL_REGEX = new RegExp(/^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*$/g);
export default function Login() {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
@@ -23,6 +25,25 @@ export default function Login() {
redirectIfFound: true,
});
const forgotPassword = () => {
if (!email || email.length < 0 || !EMAIL_REGEX.test(email)) {
toast.error("Please enter your e-mail to reset your password!", {toastId: "forgot-invalid-email"});
return;
}
axios
.post<{ok: boolean}>("/api/reset", {email})
.then((response) => {
if (response.data.ok) {
toast.success("You should receive an e-mail to reset your password!", {toastId: "forgot-success"});
return;
}
toast.error("That e-mail address is not connected to an account!", {toastId: "forgot-error"});
})
.catch(() => toast.error("That e-mail address is not connected to an account!", {toastId: "forgot-error"}));
};
const login = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
@@ -46,7 +67,7 @@ export default function Login() {
return (
<>
<Head>
<title>Login | IELTS GPT</title>
<title>Login | EnCoach</title>
<meta name="description" content="Generated by create next app" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="/favicon.ico" />
@@ -80,7 +101,9 @@ export default function Login() {
</div>
<span>Remember my password</span>
</div>
<span className="text-mti-purple-light text-xs">Forgot Password?</span>
<span className="text-mti-purple-light text-xs cursor-pointer hover:underline" onClick={forgotPassword}>
Forgot Password?
</span>
</div>
<Button className="mt-8 w-full" color="purple" disabled={isLoading}>
{!isLoading && "Login"}