import Head from 'next/head'; import {useRouter} from "next/router"; import {signIn} from 'next-auth/client' import ErrorAlert from "../../components/ui/alerts/Error"; import {useState} from "react"; import {UsernameInput} from "../../components/ui/UsernameInput"; import prisma from "../../lib/prisma"; export default function Signup(props) { const router = useRouter(); const [ hasErrors, setHasErrors ] = useState(false); const [ errorMessage, setErrorMessage ] = useState(''); const handleErrors = async (resp) => { if (!resp.ok) { const err = await resp.json(); throw new Error(err.message); } } 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; fetch('/api/auth/signup', { body: JSON.stringify({ username: e.target.username.value, password, email, }), headers: { 'Content-Type': 'application/json', }, method: 'POST' } ) .then(handleErrors) .then( () => signIn('Calendso', { callbackUrl: (router.query.callbackUrl || '') as string }) ) .catch( (err) => { setHasErrors(true); setErrorMessage(err.message); }); }; return (
Sign up

Create your account

{hasErrors && }
); } export async function getServerSideProps(ctx) { if (!ctx.query.token) { return { notFound: true, } } const verificationRequest = await prisma.verificationRequest.findUnique({ where: { token: ctx.query.token, } }); // for now, disable if no verificationRequestToken given or token expired if ( ! verificationRequest || verificationRequest.expires < new Date() ) { return { notFound: true, } } const existingUser = await prisma.user.findFirst({ where: { AND: [ { email: verificationRequest.identifier }, { emailVerified: { not: null, }, } ] } }); if (existingUser) { return { redirect: { permanent: false, destination: '/auth/login?callbackUrl=' + ctx.query.callbackUrl } }; } return { props: { email: verificationRequest.identifier } }; }