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

@@ -1,5 +1,5 @@
import {NextApiRequest, NextApiResponse} from "next"; import {NextApiRequest, NextApiResponse} from "next";
import {createUserWithEmailAndPassword, getAuth} from "firebase/auth"; import {createUserWithEmailAndPassword, getAuth, sendPasswordResetEmail} from "firebase/auth";
import {app} from "@/firebase"; import {app} from "@/firebase";
import {sessionOptions} from "@/lib/session"; import {sessionOptions} from "@/lib/session";
import {withIronSessionApiRoute} from "iron-session/next"; import {withIronSessionApiRoute} from "iron-session/next";

View File

@@ -0,0 +1,17 @@
import {NextApiRequest, NextApiResponse} from "next";
import {getAuth, sendPasswordResetEmail, confirmPasswordReset} from "firebase/auth";
import {app} from "@/firebase";
import {sessionOptions} from "@/lib/session";
import {withIronSessionApiRoute} from "iron-session/next";
const auth = getAuth(app);
export default withIronSessionApiRoute(confirm, sessionOptions);
async function confirm(req: NextApiRequest, res: NextApiResponse) {
const {code, password} = req.body as {code: string; password: string};
confirmPasswordReset(auth, code, password)
.then(() => res.status(200).json({ok: true}))
.catch(() => res.status(404).json({ok: false}));
}

View File

@@ -0,0 +1,17 @@
import {NextApiRequest, NextApiResponse} from "next";
import {getAuth, sendPasswordResetEmail} from "firebase/auth";
import {app} from "@/firebase";
import {sessionOptions} from "@/lib/session";
import {withIronSessionApiRoute} from "iron-session/next";
const auth = getAuth(app);
export default withIronSessionApiRoute(reset, sessionOptions);
async function reset(req: NextApiRequest, res: NextApiResponse) {
const {email} = req.body as {email: string};
sendPasswordResetEmail(auth, email)
.then(() => res.status(200).json({ok: true}))
.catch(() => res.status(404).json({ok: false}));
}

View File

@@ -307,7 +307,7 @@ export default function Page() {
return ( return (
<> <>
<Head> <Head>
<title>Exam | IELTS GPT</title> <title>Exam | EnCoach</title>
<meta <meta
name="description" name="description"
content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop." content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop."

View File

@@ -310,7 +310,7 @@ export default function Page() {
return ( return (
<> <>
<Head> <Head>
<title>Exercises | IELTS GPT</title> <title>Exercises | EnCoach</title>
<meta <meta
name="description" name="description"
content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop." content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop."

View File

@@ -50,7 +50,7 @@ export default function Home() {
return ( return (
<> <>
<Head> <Head>
<title>IELTS GPT | Muscat Training Institute</title> <title>EnCoach | Muscat Training Institute</title>
<meta <meta
name="description" name="description"
content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop." content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop."
@@ -68,7 +68,7 @@ export default function Home() {
return ( return (
<> <>
<Head> <Head>
<title>IELTS GPT | Muscat Training Institute</title> <title>EnCoach | Muscat Training Institute</title>
<meta <meta
name="description" name="description"
content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop." content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop."

View File

@@ -12,6 +12,8 @@ import Link from "next/link";
import Input from "@/components/Low/Input"; import Input from "@/components/Low/Input";
import clsx from "clsx"; 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() { export default function Login() {
const [email, setEmail] = useState(""); const [email, setEmail] = useState("");
const [password, setPassword] = useState(""); const [password, setPassword] = useState("");
@@ -23,6 +25,25 @@ export default function Login() {
redirectIfFound: true, 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>) => { const login = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault(); e.preventDefault();
@@ -46,7 +67,7 @@ export default function Login() {
return ( return (
<> <>
<Head> <Head>
<title>Login | IELTS GPT</title> <title>Login | EnCoach</title>
<meta name="description" content="Generated by create next app" /> <meta name="description" content="Generated by create next app" />
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="/favicon.ico" /> <link rel="icon" href="/favicon.ico" />
@@ -80,7 +101,9 @@ export default function Login() {
</div> </div>
<span>Remember my password</span> <span>Remember my password</span>
</div> </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> </div>
<Button className="mt-8 w-full" color="purple" disabled={isLoading}> <Button className="mt-8 w-full" color="purple" disabled={isLoading}>
{!isLoading && "Login"} {!isLoading && "Login"}

View File

@@ -114,7 +114,7 @@ export default function Home() {
return ( return (
<> <>
<Head> <Head>
<title>IELTS GPT | Muscat Training Institute</title> <title>EnCoach | Muscat Training Institute</title>
<meta <meta
name="description" name="description"
content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop." content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop."

View File

@@ -206,7 +206,7 @@ export default function History({user}: {user: User}) {
return ( return (
<> <>
<Head> <Head>
<title>IELTS GPT | Muscat Training Institute</title> <title>EnCoach | Muscat Training Institute</title>
<meta <meta
name="description" name="description"
content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop." content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop."

View File

@@ -48,7 +48,7 @@ export default function Register() {
return ( return (
<> <>
<Head> <Head>
<title>Register | IELTS GPT</title> <title>Register | EnCoach</title>
<meta name="description" content="Generated by create next app" /> <meta name="description" content="Generated by create next app" />
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="/favicon.ico" /> <link rel="icon" href="/favicon.ico" />

110
src/pages/reset.tsx Normal file
View File

@@ -0,0 +1,110 @@
/* eslint-disable @next/next/no-img-element */
import {User} from "@/interfaces/user";
import {toast, ToastContainer} from "react-toastify";
import axios from "axios";
import {FormEvent, useState} from "react";
import Head from "next/head";
import useUser from "@/hooks/useUser";
import {Divider} from "primereact/divider";
import Button from "@/components/Low/Button";
import {BsArrowRepeat, BsCheck} from "react-icons/bs";
import Link from "next/link";
import Input from "@/components/Low/Input";
import clsx from "clsx";
import {useRouter} from "next/router";
export function getServerSideProps({query, res}: {query: {oobCode: string}; res: any}) {
if (!query || !query.oobCode) {
res.setHeader("location", "/login");
res.statusCode = 302;
res.end();
return {
props: {},
};
}
return {
props: {
code: query.oobCode,
},
};
}
export default function Reset({code}: {code: string}) {
const [password, setPassword] = useState("");
const [isLoading, setIsLoading] = useState(false);
const router = useRouter();
useUser({
redirectTo: "/",
redirectIfFound: true,
});
const login = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
setIsLoading(true);
axios
.post<{ok: boolean}>("/api/reset/confirm", {code, password})
.then((response) => {
if (response.data.ok) {
toast.success("Your password has been reset!", {toastId: "reset-successful"});
setTimeout(() => {
router.push("/login");
}, 2000);
return;
}
toast.error("Something went wrong! Please make sure to click the link in your e-mail again!", {toastId: "reset-error"});
})
.catch(() => {
toast.error("Something went wrong! Please make sure to click the link in your e-mail again!", {toastId: "reset-error"});
})
.finally(() => setIsLoading(false));
};
return (
<>
<Head>
<title>Reset | 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" />
</Head>
<main className="w-full h-[100vh] flex bg-white text-black">
<ToastContainer />
<section className="h-full w-fit min-w-fit relative hidden lg:flex">
<div className="absolute h-full w-full bg-mti-rose-light z-10 bg-opacity-50" />
<img src="/people-talking-tablet.png" alt="People smiling looking at a tablet" className="h-full aspect-auto" />
</section>
<section className="h-full w-full flex flex-col items-center justify-center gap-2">
<div className="flex flex-col gap-2 items-center relative">
<img src="/logo_title.png" alt="EnCoach's Logo" className="w-36 lg:w-64 absolute -top-36 lg:-top-64" />
<h1 className="font-bold text-2xl lg:text-4xl">Reset your password</h1>
<p className="self-start text-sm lg:text-base font-normal text-mti-gray-cool">to your registered Email Address</p>
</div>
<Divider className="max-w-xs lg:max-w-md" />
<form className="flex flex-col items-center gap-6 w-full -lg:px-8 lg:w-1/2" onSubmit={login}>
<Input type="password" name="password" onChange={(e) => setPassword(e)} placeholder="Password" />
<Button className="mt-8 w-full" color="purple" disabled={isLoading}>
{!isLoading && "Reset"}
{isLoading && (
<div className="flex items-center justify-center">
<BsArrowRepeat className="text-white animate-spin" size={25} />
</div>
)}
</Button>
</form>
<span className="text-mti-gray-cool text-sm font-normal mt-8">
Don&apos;t have an account?{" "}
<Link className="text-mti-purple-light" href="/register">
Sign up
</Link>
</span>
</section>
</main>
</>
);
}

View File

@@ -57,7 +57,7 @@ export default function Stats() {
return ( return (
<> <>
<Head> <Head>
<title>Stats | IELTS GPT</title> <title>Stats | EnCoach</title>
<meta <meta
name="description" name="description"
content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop." content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop."

View File

@@ -58,7 +58,7 @@ export default function Page() {
return ( return (
<> <>
<Head> <Head>
<title>Exam | IELTS GPT</title> <title>Exam | EnCoach</title>
<meta <meta
name="description" name="description"
content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop." content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop."

View File

@@ -57,7 +57,7 @@ export default function Users({user}: {user: User}) {
return ( return (
<> <>
<Head> <Head>
<title>IELTS GPT | Users</title> <title>EnCoach | Users</title>
<meta <meta
name="description" name="description"
content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop." content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop."