ENCOA-100: Changed the login/register photo

This commit is contained in:
Tiago Ribeiro
2024-08-22 16:49:27 +01:00
parent 192324b891
commit 4505ea5ff8
4 changed files with 151 additions and 200 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

BIN
public/red-stock-photo.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 MiB

View File

@@ -1,229 +1,181 @@
/* eslint-disable @next/next/no-img-element */ /* eslint-disable @next/next/no-img-element */
import { User } from "@/interfaces/user"; import {User} from "@/interfaces/user";
import { toast, ToastContainer } from "react-toastify"; import {toast, ToastContainer} from "react-toastify";
import axios from "axios"; import axios from "axios";
import { FormEvent, useEffect, useState } from "react"; import {FormEvent, useEffect, useState} from "react";
import Head from "next/head"; import Head from "next/head";
import useUser from "@/hooks/useUser"; import useUser from "@/hooks/useUser";
import { Divider } from "primereact/divider"; import {Divider} from "primereact/divider";
import Button from "@/components/Low/Button"; import Button from "@/components/Low/Button";
import { BsArrowRepeat, BsCheck } from "react-icons/bs"; import {BsArrowRepeat, BsCheck} from "react-icons/bs";
import Link from "next/link"; 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";
import { useRouter } from "next/router"; import {useRouter} from "next/router";
import EmailVerification from "./(auth)/EmailVerification"; import EmailVerification from "./(auth)/EmailVerification";
import { withIronSessionSsr } from "iron-session/next"; import {withIronSessionSsr} from "iron-session/next";
import { sessionOptions } from "@/lib/session"; import {sessionOptions} from "@/lib/session";
const EMAIL_REGEX = new RegExp( const EMAIL_REGEX = new RegExp(/^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*$/g);
/^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*$/g,
);
export const getServerSideProps = withIronSessionSsr(({ req, res }) => { export const getServerSideProps = withIronSessionSsr(({req, res}) => {
const user = req.session.user; const user = req.session.user;
const envVariables: { [key: string]: string } = {}; const envVariables: {[key: string]: string} = {};
Object.keys(process.env) Object.keys(process.env)
.filter((x) => x.startsWith("NEXT_PUBLIC")) .filter((x) => x.startsWith("NEXT_PUBLIC"))
.forEach((x: string) => { .forEach((x: string) => {
envVariables[x] = process.env[x]!; envVariables[x] = process.env[x]!;
}); });
if (user && user.isVerified) { if (user && user.isVerified) {
return { return {
redirect: { redirect: {
destination: "/", destination: "/",
permanent: false, permanent: false,
} },
}; };
} }
return { return {
props: { user: null, envVariables }, props: {user: null, envVariables},
}; };
}, sessionOptions); }, sessionOptions);
export default function Login() { export default function Login() {
const [email, setEmail] = useState(""); const [email, setEmail] = useState("");
const [password, setPassword] = useState(""); const [password, setPassword] = useState("");
const [rememberPassword, setRememberPassword] = useState(false); const [rememberPassword, setRememberPassword] = useState(false);
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const router = useRouter(); const router = useRouter();
const { user, mutateUser } = useUser({ const {user, mutateUser} = useUser({
redirectTo: "/", redirectTo: "/",
redirectIfFound: true, redirectIfFound: true,
}); });
useEffect(() => { useEffect(() => {
if (user && user.isVerified) router.push("/"); if (user && user.isVerified) router.push("/");
}, [router, user]); }, [router, user]);
const forgotPassword = () => { const forgotPassword = () => {
if (!email || email.length < 0 || !EMAIL_REGEX.test(email)) { if (!email || email.length < 0 || !EMAIL_REGEX.test(email)) {
toast.error("Please enter your e-mail to reset your password!", { toast.error("Please enter your e-mail to reset your password!", {
toastId: "forgot-invalid-email", toastId: "forgot-invalid-email",
}); });
return; return;
} }
axios axios
.post<{ ok: boolean }>("/api/reset", { email }) .post<{ok: boolean}>("/api/reset", {email})
.then((response) => { .then((response) => {
if (response.data.ok) { if (response.data.ok) {
toast.success( toast.success("You should receive an e-mail to reset your password!", {toastId: "forgot-success"});
"You should receive an e-mail to reset your password!", return;
{ toastId: "forgot-success" }, }
);
return;
}
toast.error("That e-mail address is not connected to an account!", { toast.error("That e-mail address is not connected to an account!", {
toastId: "forgot-error", toastId: "forgot-error",
}); });
}) })
.catch(() => .catch(() =>
toast.error("That e-mail address is not connected to an account!", { toast.error("That e-mail address is not connected to an account!", {
toastId: "forgot-error", toastId: "forgot-error",
}), }),
); );
}; };
const login = (e: FormEvent<HTMLFormElement>) => { const login = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault(); e.preventDefault();
setIsLoading(true); setIsLoading(true);
axios axios
.post<User>("/api/login", { email, password }) .post<User>("/api/login", {email, password})
.then((response) => { .then((response) => {
toast.success("You have been logged in!", { toast.success("You have been logged in!", {
toastId: "login-successful", toastId: "login-successful",
}); });
mutateUser(response.data); mutateUser(response.data);
}) })
.catch((e) => { .catch((e) => {
if (e.response.status === 401) { if (e.response.status === 401) {
toast.error("Wrong login credentials!", { toast.error("Wrong login credentials!", {
toastId: "wrong-credentials", toastId: "wrong-credentials",
}); });
} else { } else {
toast.error("Something went wrong!", { toastId: "server-error" }); toast.error("Something went wrong!", {toastId: "server-error"});
} }
setIsLoading(false); setIsLoading(false);
}) })
.finally(() => setIsLoading(false)); .finally(() => setIsLoading(false));
}; };
return ( return (
<> <>
<Head> <Head>
<title>Login | EnCoach</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" />
</Head> </Head>
<main className="flex h-[100vh] w-full bg-white text-black"> <main className="flex h-[100vh] w-full bg-white text-black">
<ToastContainer /> <ToastContainer />
<section className="relative hidden h-full w-fit min-w-fit lg:flex"> <section className="relative hidden h-full w-fit min-w-fit lg:flex">
<div className="bg-mti-rose-light absolute z-10 h-full w-full bg-opacity-50" /> {/* <div className="bg-mti-rose-light absolute z-10 h-full w-full bg-opacity-50" /> */}
<img <img src="/red-stock-photo.jpg" alt="People smiling looking at a tablet" className="aspect-auto h-full" />
src="/people-talking-tablet.png" </section>
alt="People smiling looking at a tablet" <section className="flex h-full w-full flex-col items-center justify-center gap-2">
className="aspect-auto h-full" <div className={clsx("flex flex-col items-center", !user && "mb-4")}>
/> <img src="/logo_title.png" alt="EnCoach's Logo" className="w-36 lg:w-56" />
</section> <h1 className="text-2xl font-bold lg:text-4xl">Login to your account</h1>
<section className="flex h-full w-full flex-col items-center justify-center gap-2"> <p className="text-mti-gray-cool self-start text-sm font-normal lg:text-base">with your registered Email Address</p>
<div className={clsx("flex flex-col items-center", !user && "mb-4")}> </div>
<img <Divider className="max-w-xs lg:max-w-md" />
src="/logo_title.png" {!user && (
alt="EnCoach's Logo" <>
className="w-36 lg:w-56" <form className="-lg:px-8 flex w-full flex-col items-center gap-6 lg:w-1/2" onSubmit={login}>
/> <Input type="email" name="email" onChange={(e) => setEmail(e.toLowerCase())} placeholder="Enter email address" />
<h1 className="text-2xl font-bold lg:text-4xl"> <Input type="password" name="password" onChange={(e) => setPassword(e)} placeholder="Password" />
Login to your account <div className="flex w-full justify-between px-4">
</h1> <div
<p className="text-mti-gray-cool self-start text-sm font-normal lg:text-base"> className="text-mti-gray-dim flex cursor-pointer gap-3 text-xs"
with your registered Email Address onClick={() => setRememberPassword((prev) => !prev)}>
</p> <input type="checkbox" className="hidden" />
</div> <div
<Divider className="max-w-xs lg:max-w-md" /> className={clsx(
{!user && ( "border-mti-purple-light flex h-4 w-4 items-center justify-center rounded-sm border bg-white",
<> "transition duration-300 ease-in-out",
<form rememberPassword && "!bg-mti-purple-light ",
className="-lg:px-8 flex w-full flex-col items-center gap-6 lg:w-1/2" )}>
onSubmit={login} <BsCheck color="white" className="h-full w-full" />
> </div>
<Input <span>Remember my password</span>
type="email" </div>
name="email" <span className="text-mti-purple-light cursor-pointer text-xs hover:underline" onClick={forgotPassword}>
onChange={(e) => setEmail(e.toLowerCase())} Forgot Password?
placeholder="Enter email address" </span>
/> </div>
<Input <Button className="mt-8 w-full" color="purple" disabled={isLoading}>
type="password" {!isLoading && "Login"}
name="password" {isLoading && (
onChange={(e) => setPassword(e)} <div className="flex items-center justify-center">
placeholder="Password" <BsArrowRepeat className="animate-spin text-white" size={25} />
/> </div>
<div className="flex w-full justify-between px-4"> )}
<div </Button>
className="text-mti-gray-dim flex cursor-pointer gap-3 text-xs" </form>
onClick={() => setRememberPassword((prev) => !prev)} <span className="text-mti-gray-cool mt-8 text-sm font-normal">
> Don&apos;t have an account?{" "}
<input type="checkbox" className="hidden" /> <Link className="text-mti-purple-light" href="/register">
<div Sign up
className={clsx( </Link>
"border-mti-purple-light flex h-4 w-4 items-center justify-center rounded-sm border bg-white", </span>
"transition duration-300 ease-in-out", </>
rememberPassword && "!bg-mti-purple-light ", )}
)} {user && !user.isVerified && <EmailVerification user={user} isLoading={isLoading} setIsLoading={setIsLoading} />}
> </section>
<BsCheck color="white" className="h-full w-full" /> </main>
</div> </>
<span>Remember my password</span> );
</div>
<span
className="text-mti-purple-light cursor-pointer text-xs hover:underline"
onClick={forgotPassword}
>
Forgot Password?
</span>
</div>
<Button
className="mt-8 w-full"
color="purple"
disabled={isLoading}
>
{!isLoading && "Login"}
{isLoading && (
<div className="flex items-center justify-center">
<BsArrowRepeat
className="animate-spin text-white"
size={25}
/>
</div>
)}
</Button>
</form>
<span className="text-mti-gray-cool mt-8 text-sm font-normal">
Don&apos;t have an account?{" "}
<Link className="text-mti-purple-light" href="/register">
Sign up
</Link>
</span>
</>
)}
{user && !user.isVerified && (
<EmailVerification
user={user}
isLoading={isLoading}
setIsLoading={setIsLoading}
/>
)}
</section>
</main>
</>
);
} }

View File

@@ -55,8 +55,7 @@ export default function Register({code: queryCode}: {code: string}) {
<main className="w-full h-[100vh] flex bg-white text-black"> <main className="w-full h-[100vh] flex bg-white text-black">
<ToastContainer /> <ToastContainer />
<section className="h-full w-fit min-w-fit relative hidden lg:flex"> <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="/red-stock-photo.jpg" alt="People smiling looking at a tablet" className="aspect-auto h-full" />
<img src="/people-talking-tablet.png" alt="People smiling looking at a tablet" className="h-full aspect-auto" />
</section> </section>
<section className="h-full w-full flex flex-col items-center justify-center gap-4"> <section className="h-full w-full flex flex-col items-center justify-center gap-4">
<div className={clsx("flex flex-col items-center", !user && "mb-4")}> <div className={clsx("flex flex-col items-center", !user && "mb-4")}>