/* eslint-disable @next/next/no-img-element */ import {toast, ToastContainer} from "react-toastify"; import {useState} from "react"; import Head from "next/head"; import useUser from "@/hooks/useUser"; import Button from "@/components/Low/Button"; import {BsArrowRepeat} from "react-icons/bs"; import Link from "next/link"; import Input from "@/components/Low/Input"; import axios from "axios"; import {Divider} from "primereact/divider"; import {useRouter} from "next/router"; import clsx from "clsx"; export default function Register() { const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [isLoading, setIsLoading] = useState(false); const router = useRouter(); const {user, mutateUser} = useUser({ redirectTo: "/", redirectIfFound: true, }); const register = (e: any) => { e.preventDefault(); if (confirmPassword !== password) { toast.error("Your passwords do not match!", {toastId: "password-not-match"}); return; } setIsLoading(true); axios .post("/api/register", { name, email, password, profilePicture: "/defaultAvatar.png", }) .then((response) => { mutateUser(response.data.user).then(sendEmailVerification); }) .catch((error) => { console.log(error.response.data); if (error.response.status === 401) { toast.error("There is already a user with that e-mail!"); return; } toast.error("There was something wrong, please try again!"); }) .finally(() => setIsLoading(false)); }; const sendEmailVerification = () => { setIsLoading(true); axios .post<{ok: boolean}>("/api/reset/sendVerification", {}) .catch((e) => { console.log(e); toast.error("Something went wrong, please logout and re-login.", {toastId: "send-verify-error"}); }) .finally(() => setIsLoading(false)); }; const logout = async () => { axios.post("/api/logout").finally(() => { setTimeout(() => router.reload(), 500); }); }; const initialStep = () => ( <>
setName(e)} placeholder="Enter your name" defaultValue={name} required /> setEmail(e)} placeholder="Enter email address" defaultValue={email} required /> setPassword(e)} placeholder="Enter your password" defaultValue={password} required /> setConfirmPassword(e)} placeholder="Confirm your password" defaultValue={confirmPassword} required />
); return ( <> Register | EnCoach
People smiling looking at a tablet
EnCoach's Logo

Create new account

{!user && ( <> {initialStep()} Sign in instead )} {user && !user.isVerified && ( <>

Please confirm your account!

An e-mail has been sent to {user.email}, please click the link in it to confirm your account to be able to use the application.

Please refresh this page once it has been verified.
)}
); }