import { getCsrfToken, signIn } from "next-auth/client"; import Link from "next/link"; import { useRouter } from "next/router"; import { useState } from "react"; import { ErrorCode, getSession } from "@lib/auth"; import Loader from "@components/Loader"; import { HeadSeo } from "@components/seo/head-seo"; const errorMessages: { [key: string]: string } = { [ErrorCode.SecondFactorRequired]: "Two-factor authentication enabled. Please enter the six-digit code from your authenticator app.", [ErrorCode.IncorrectPassword]: "Password is incorrect. Please try again.", [ErrorCode.UserNotFound]: "No account exists matching that email address.", [ErrorCode.IncorrectTwoFactorCode]: "Two-factor code is incorrect. Please try again.", [ErrorCode.InternalServerError]: "Something went wrong. Please try again and contact us if the issue persists.", }; export default function Login({ csrfToken }) { const router = useRouter(); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [code, setCode] = useState(""); const [isSubmitting, setIsSubmitting] = useState(false); const [secondFactorRequired, setSecondFactorRequired] = useState(false); const [errorMessage, setErrorMessage] = useState(null); const callbackUrl = typeof router.query?.callbackUrl === "string" ? router.query.callbackUrl : "/"; async function handleSubmit(e: React.SyntheticEvent) { e.preventDefault(); if (isSubmitting) { return; } setIsSubmitting(true); setErrorMessage(null); try { const response = await signIn("credentials", { redirect: false, email, password, totpCode: code, callbackUrl, }); if (!response) { console.error("Received empty response from next auth"); return; } if (!response.error) { router.replace(callbackUrl); return; } if (response.error === ErrorCode.SecondFactorRequired) { setSecondFactorRequired(true); setErrorMessage(errorMessages[ErrorCode.SecondFactorRequired]); } else { setErrorMessage(errorMessages[response.error] || "Something went wrong."); } } catch (e) { setErrorMessage("Something went wrong."); } finally { setIsSubmitting(false); } } return (
{isSubmitting && (
)}
Cal.com Logo

Sign in to your account

setEmail(e.currentTarget.value)} className="appearance-none block w-full px-3 py-2 border border-neutral-300 rounded-sm shadow-sm placeholder-gray-400 focus:outline-none focus:ring-neutral-900 focus:border-neutral-900 sm:text-sm" />
setPassword(e.currentTarget.value)} className="appearance-none block w-full px-3 py-2 border border-neutral-300 rounded-sm shadow-sm placeholder-gray-400 focus:outline-none focus:ring-neutral-900 focus:border-neutral-900 sm:text-sm" />
{secondFactorRequired && (
setCode(e.currentTarget.value)} className="appearance-none block w-full px-3 py-2 border border-neutral-300 rounded-sm shadow-sm placeholder-gray-400 focus:outline-none focus:ring-neutral-900 focus:border-neutral-900 sm:text-sm" />
)}
{errorMessage &&

{errorMessage}

}
Don't have an account? {/* replace this with your account creation flow */} Create an account
); } Login.getInitialProps = async (context) => { const { req, res } = context; const session = await getSession({ req }); if (session) { res.writeHead(302, { Location: "/" }); res.end(); return; } return { csrfToken: await getCsrfToken(context), }; };