2021-08-02 20:51:57 +00:00
|
|
|
import { signIn } from "next-auth/client";
|
2021-09-22 19:52:38 +00:00
|
|
|
import { useRouter } from "next/router";
|
2021-08-02 20:51:57 +00:00
|
|
|
import { useState } from "react";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-08-27 12:35:20 +00:00
|
|
|
import prisma from "@lib/prisma";
|
2021-06-09 21:29:31 +00:00
|
|
|
|
2021-09-22 19:52:38 +00:00
|
|
|
import { HeadSeo } from "@components/seo/head-seo";
|
|
|
|
import { UsernameInput } from "@components/ui/UsernameInput";
|
|
|
|
import ErrorAlert from "@components/ui/alerts/Error";
|
|
|
|
|
2021-06-09 21:29:31 +00:00
|
|
|
export default function Signup(props) {
|
|
|
|
const router = useRouter();
|
|
|
|
|
2021-08-02 20:51:57 +00:00
|
|
|
const [hasErrors, setHasErrors] = useState(false);
|
|
|
|
const [errorMessage, setErrorMessage] = useState("");
|
2021-06-09 21:29:31 +00:00
|
|
|
|
|
|
|
const handleErrors = async (resp) => {
|
|
|
|
if (!resp.ok) {
|
|
|
|
const err = await resp.json();
|
|
|
|
throw new Error(err.message);
|
|
|
|
}
|
2021-08-02 20:51:57 +00:00
|
|
|
};
|
2021-06-09 21:29:31 +00:00
|
|
|
|
|
|
|
const signUp = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
if (e.target.password.value !== e.target.passwordcheck.value) {
|
|
|
|
throw new Error("Password mismatch");
|
|
|
|
}
|
|
|
|
|
|
|
|
const email: string = e.target.email.value;
|
|
|
|
const password: string = e.target.password.value;
|
|
|
|
|
2021-08-02 20:51:57 +00:00
|
|
|
fetch("/api/auth/signup", {
|
|
|
|
body: JSON.stringify({
|
|
|
|
username: e.target.username.value,
|
|
|
|
password,
|
|
|
|
email,
|
|
|
|
}),
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
method: "POST",
|
|
|
|
})
|
2021-06-09 21:29:31 +00:00
|
|
|
.then(handleErrors)
|
2021-09-21 09:59:34 +00:00
|
|
|
.then(() => signIn("Cal.com", { callbackUrl: (router.query.callbackUrl || "") as string }))
|
2021-08-02 20:51:57 +00:00
|
|
|
.catch((err) => {
|
2021-06-09 21:29:31 +00:00
|
|
|
setHasErrors(true);
|
|
|
|
setErrorMessage(err.message);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2021-08-02 20:51:57 +00:00
|
|
|
<div
|
|
|
|
className="min-h-screen bg-gray-50 flex flex-col justify-center py-12 sm:px-6 lg:px-8"
|
|
|
|
aria-labelledby="modal-title"
|
|
|
|
role="dialog"
|
|
|
|
aria-modal="true">
|
2021-08-27 12:35:20 +00:00
|
|
|
<HeadSeo title="Sign up" description="Sign up" />
|
2021-06-09 21:29:31 +00:00
|
|
|
<div className="sm:mx-auto sm:w-full sm:max-w-md">
|
2021-08-02 20:51:57 +00:00
|
|
|
<h2 className="text-center text-3xl font-extrabold text-gray-900">Create your account</h2>
|
2021-06-09 21:29:31 +00:00
|
|
|
</div>
|
|
|
|
<div className="mt-8 sm:mx-auto sm:w-full sm:max-w-md">
|
|
|
|
<div className="bg-white py-8 px-4 shadow mx-2 sm:rounded-lg sm:px-10">
|
|
|
|
<form method="POST" onSubmit={signUp} className="bg-white space-y-6">
|
|
|
|
{hasErrors && <ErrorAlert message={errorMessage} />}
|
|
|
|
<div>
|
|
|
|
<div className="mb-2">
|
|
|
|
<UsernameInput required />
|
|
|
|
</div>
|
|
|
|
<div className="mb-2">
|
2021-08-02 20:51:57 +00:00
|
|
|
<label htmlFor="email" className="block text-sm font-medium text-gray-700">
|
|
|
|
Email
|
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
type="email"
|
|
|
|
name="email"
|
|
|
|
id="email"
|
|
|
|
placeholder="jdoe@example.com"
|
|
|
|
disabled={!!props.email}
|
|
|
|
readOnly={!!props.email}
|
|
|
|
value={props.email}
|
|
|
|
className="bg-gray-100 mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-black focus:border-black sm:text-sm"
|
|
|
|
/>
|
2021-06-09 21:29:31 +00:00
|
|
|
</div>
|
|
|
|
<div className="mb-2">
|
2021-08-02 20:51:57 +00:00
|
|
|
<label htmlFor="password" className="block text-sm font-medium text-gray-700">
|
|
|
|
Password
|
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
type="password"
|
|
|
|
name="password"
|
|
|
|
id="password"
|
|
|
|
required
|
|
|
|
placeholder="•••••••••••••"
|
|
|
|
className="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-black focus:border-black sm:text-sm"
|
|
|
|
/>
|
2021-06-09 21:29:31 +00:00
|
|
|
</div>
|
|
|
|
<div>
|
2021-08-02 20:51:57 +00:00
|
|
|
<label htmlFor="passwordcheck" className="block text-sm font-medium text-gray-700">
|
|
|
|
Confirm password
|
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
type="password"
|
|
|
|
name="passwordcheck"
|
|
|
|
id="passwordcheck"
|
|
|
|
required
|
|
|
|
placeholder="•••••••••••••"
|
|
|
|
className="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-black focus:border-black sm:text-sm"
|
|
|
|
/>
|
2021-06-09 21:29:31 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="mt-3 sm:mt-4 flex">
|
2021-08-02 20:51:57 +00:00
|
|
|
<input
|
|
|
|
type="submit"
|
|
|
|
value="Create Account"
|
|
|
|
className="btn btn-primary w-7/12 mr-2 inline-flex justify-center rounded-md border border-transparent cursor-pointer shadow-sm px-4 py-2 bg-blue-600 text-base font-medium text-white hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-black sm:text-sm"
|
|
|
|
/>
|
|
|
|
<a
|
2021-09-21 10:36:29 +00:00
|
|
|
onClick={() => signIn("Cal.com", { callbackUrl: (router.query.callbackUrl || "") as string })}
|
2021-08-02 20:51:57 +00:00
|
|
|
className="w-5/12 inline-flex justify-center text-sm text-gray-500 font-medium border px-4 py-2 rounded btn cursor-pointer">
|
|
|
|
Login instead
|
|
|
|
</a>
|
2021-06-09 21:29:31 +00:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getServerSideProps(ctx) {
|
|
|
|
if (!ctx.query.token) {
|
|
|
|
return {
|
|
|
|
notFound: true,
|
2021-08-02 20:51:57 +00:00
|
|
|
};
|
2021-06-09 21:29:31 +00:00
|
|
|
}
|
|
|
|
const verificationRequest = await prisma.verificationRequest.findUnique({
|
|
|
|
where: {
|
|
|
|
token: ctx.query.token,
|
2021-08-02 20:51:57 +00:00
|
|
|
},
|
2021-06-09 21:29:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// for now, disable if no verificationRequestToken given or token expired
|
2021-08-02 20:51:57 +00:00
|
|
|
if (!verificationRequest || verificationRequest.expires < new Date()) {
|
2021-06-09 21:29:31 +00:00
|
|
|
return {
|
|
|
|
notFound: true,
|
2021-08-02 20:51:57 +00:00
|
|
|
};
|
2021-06-09 21:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const existingUser = await prisma.user.findFirst({
|
|
|
|
where: {
|
|
|
|
AND: [
|
|
|
|
{
|
2021-08-02 20:51:57 +00:00
|
|
|
email: verificationRequest.identifier,
|
2021-06-09 21:29:31 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
emailVerified: {
|
|
|
|
not: null,
|
|
|
|
},
|
2021-08-02 20:51:57 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2021-06-09 21:29:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (existingUser) {
|
2021-08-02 20:51:57 +00:00
|
|
|
return {
|
|
|
|
redirect: { permanent: false, destination: "/auth/login?callbackUrl=" + ctx.query.callbackUrl },
|
|
|
|
};
|
2021-06-09 21:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return { props: { email: verificationRequest.identifier } };
|
2021-08-02 20:51:57 +00:00
|
|
|
}
|