/* eslint-disable @next/next/no-img-element */ import { User } from "@/interfaces/user"; import { toast, ToastContainer } from "react-toastify"; import axios from "axios"; import { FormEvent, useEffect, useMemo, 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"; import EmailVerification from "./(auth)/EmailVerification"; import { withIronSessionSsr } from "iron-session/next"; import { sessionOptions } from "@/lib/session"; import { requestUser } from "@/utils/api"; import { redirect } from "@/utils"; const EMAIL_REGEX = new RegExp(/^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*$/g); export const getServerSideProps = withIronSessionSsr(async ({ req, res, query }) => { const destination = !query.destination ? "/" : Buffer.from(query.destination as string, 'base64').toString() const user = await requestUser(req, res) if (user && user.isVerified) return redirect(destination) return { props: { user: null, destination }, }; }, sessionOptions); export default function Login({ destination = "/" }: { destination?: string }) { const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [rememberPassword, setRememberPassword] = useState(false); const [isLoading, setIsLoading] = useState(false); const router = useRouter(); const isOfficialExamLogin = useMemo(() => destination.startsWith("/official-exam"), [destination]) const { user, mutateUser } = useUser(); useEffect(() => { if (user && user.isVerified) router.push(destination); }, [router, user, destination]); 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) => { e.preventDefault(); setIsLoading(true); axios .post("/api/login", { email, password }) .then((response) => { toast.success("You have been logged in!", { toastId: "login-successful", }); mutateUser(response.data, { revalidate: false }); router.push(destination); }) .catch((e) => { if (e.response.status === 401) { toast.error("Wrong login credentials!", { toastId: "wrong-credentials", }); } else { toast.error("Something went wrong!", { toastId: "server-error" }); } setIsLoading(false); }) .finally(() => setIsLoading(false)); }; return ( <> Login | EnCoach
{!isOfficialExamLogin && ( People smiling looking at a tablet )} {isOfficialExamLogin && ( People smiling looking at a tablet )}
EnCoach's Logo {!isOfficialExamLogin && ( <>

Login to your account

with your registered Email Address

)} {isOfficialExamLogin && (

Welcome to the Official Exams Portal

)}
{!user && ( <>
setEmail(e.toLowerCase())} placeholder="Enter email address" /> setPassword(e)} placeholder="Password" />
setRememberPassword((prev) => !prev)}>
Remember my password
Forgot Password?
Don't have an account?{" "} Sign up )} {user && !user.isVerified && }
); }