Made it so users have to verify their e-mail starting to use the application
This commit is contained in:
153
src/pages/action.tsx
Normal file
153
src/pages/action.tsx
Normal file
@@ -0,0 +1,153 @@
|
||||
/* 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, 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; mode: string; apiKey?: string; continueUrl?: string}; res: any}) {
|
||||
if (!query || !query.oobCode || !query.mode) {
|
||||
res.setHeader("location", "/login");
|
||||
res.statusCode = 302;
|
||||
res.end();
|
||||
return {
|
||||
props: {},
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
props: {
|
||||
code: query.oobCode,
|
||||
mode: query.mode,
|
||||
apiKey: query.apiKey,
|
||||
continueUrl: query.continueUrl,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export default function Reset({code, mode, apiKey, continueUrl}: {code: string; mode: string; apiKey?: string; continueUrl?: string}) {
|
||||
const [password, setPassword] = useState("");
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
|
||||
const router = useRouter();
|
||||
|
||||
useUser({
|
||||
redirectTo: "/",
|
||||
redirectIfFound: true,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (mode === "signIn") {
|
||||
axios
|
||||
.post<{ok: boolean}>("/api/reset/verify", {
|
||||
link: `https://encoach.com/action?apiKey=${apiKey}&mode=${mode}&oobCode=${code}&continueUrl=${continueUrl}`,
|
||||
})
|
||||
.then((response) => {
|
||||
if (response.data.ok) {
|
||||
toast.success("Your account has been verified!", {toastId: "verify-successful"});
|
||||
setTimeout(() => {
|
||||
router.push("/");
|
||||
}, 2000);
|
||||
return;
|
||||
}
|
||||
|
||||
toast.error("Something went wrong! Please make sure to click the link in your e-mail again!", {toastId: "verify-error"});
|
||||
})
|
||||
.catch(() => {
|
||||
toast.error("Something went wrong! Please make sure to click the link in your e-mail again!", {toastId: "verify-error"});
|
||||
})
|
||||
.finally(() => setIsLoading(false));
|
||||
}
|
||||
}, [apiKey, code, continueUrl, mode, router]);
|
||||
|
||||
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>
|
||||
{mode === "reset" && (
|
||||
<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't have an account?{" "}
|
||||
<Link className="text-mti-purple-light" href="/register">
|
||||
Sign up
|
||||
</Link>
|
||||
</span>
|
||||
</section>
|
||||
)}
|
||||
{mode === "signIn" && (
|
||||
<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">Confirm your account</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}>
|
||||
Your e-mail is currently being verified, please wait a second. <br /> <br />
|
||||
Once it has been verified, you will be redirected to the home page.
|
||||
</form>
|
||||
</section>
|
||||
)}
|
||||
</main>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user