diff --git a/src/pages/api/register.ts b/src/pages/api/register.ts index 16ea5228..97176c9a 100644 --- a/src/pages/api/register.ts +++ b/src/pages/api/register.ts @@ -1,5 +1,5 @@ import {NextApiRequest, NextApiResponse} from "next"; -import {createUserWithEmailAndPassword, getAuth} from "firebase/auth"; +import {createUserWithEmailAndPassword, getAuth, sendPasswordResetEmail} from "firebase/auth"; import {app} from "@/firebase"; import {sessionOptions} from "@/lib/session"; import {withIronSessionApiRoute} from "iron-session/next"; diff --git a/src/pages/api/reset/confirm.ts b/src/pages/api/reset/confirm.ts new file mode 100644 index 00000000..c61c041e --- /dev/null +++ b/src/pages/api/reset/confirm.ts @@ -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})); +} diff --git a/src/pages/api/reset/index.ts b/src/pages/api/reset/index.ts new file mode 100644 index 00000000..afb5785b --- /dev/null +++ b/src/pages/api/reset/index.ts @@ -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})); +} diff --git a/src/pages/exam.tsx b/src/pages/exam.tsx index 848dd05a..5c191a93 100644 --- a/src/pages/exam.tsx +++ b/src/pages/exam.tsx @@ -307,7 +307,7 @@ export default function Page() { return ( <> - Exam | IELTS GPT + Exam | EnCoach - Exercises | IELTS GPT + Exercises | EnCoach - IELTS GPT | Muscat Training Institute + EnCoach | Muscat Training Institute - IELTS GPT | Muscat Training Institute + EnCoach | Muscat Training Institute { + 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) => { e.preventDefault(); @@ -46,7 +67,7 @@ export default function Login() { return ( <> - Login | IELTS GPT + Login | EnCoach @@ -80,7 +101,9 @@ export default function Login() { Remember my password - Forgot Password? + + Forgot Password? + + + + Don't have an account?{" "} + + Sign up + + + + + + ); +} diff --git a/src/pages/stats.tsx b/src/pages/stats.tsx index 403d7151..82186557 100644 --- a/src/pages/stats.tsx +++ b/src/pages/stats.tsx @@ -57,7 +57,7 @@ export default function Stats() { return ( <> - Stats | IELTS GPT + Stats | EnCoach - Exam | IELTS GPT + Exam | EnCoach - IELTS GPT | Users + EnCoach | Users