2021-08-27 12:35:20 +00:00
|
|
|
import { HeadSeo } from "@components/seo/head-seo";
|
2021-06-24 15:59:11 +00:00
|
|
|
import Link from "next/link";
|
2021-09-21 09:29:20 +00:00
|
|
|
import { getCsrfToken, signIn } from "next-auth/client";
|
|
|
|
import { ErrorCode, getSession } from "@lib/auth";
|
|
|
|
import { useEffect, useState } from "react";
|
2021-09-12 08:51:59 +00:00
|
|
|
import { useRouter } from "next/router";
|
2021-09-21 09:29:20 +00:00
|
|
|
|
|
|
|
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.",
|
|
|
|
};
|
|
|
|
|
2021-03-29 21:01:12 +00:00
|
|
|
export default function Login({ csrfToken }) {
|
2021-09-12 08:51:59 +00:00
|
|
|
const router = useRouter();
|
2021-09-21 09:29:20 +00:00
|
|
|
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<string | null>(null);
|
|
|
|
|
2021-09-12 08:51:59 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (!router.query?.callbackUrl) {
|
|
|
|
window.history.replaceState(null, document.title, "?callbackUrl=/");
|
|
|
|
}
|
|
|
|
}, [router.query]);
|
|
|
|
|
2021-09-21 09:29:20 +00:00
|
|
|
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 });
|
|
|
|
if (!response) {
|
|
|
|
console.error("Received empty response from next auth");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!response.error) {
|
|
|
|
window.location.reload();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 21:01:12 +00:00
|
|
|
return (
|
2021-07-30 23:05:38 +00:00
|
|
|
<div className="min-h-screen bg-neutral-50 flex flex-col justify-center py-12 sm:px-6 lg:px-8">
|
2021-08-27 12:35:20 +00:00
|
|
|
<HeadSeo title="Login" description="Login" />
|
2021-06-24 15:59:11 +00:00
|
|
|
<div className="sm:mx-auto sm:w-full sm:max-w-md">
|
2021-09-21 09:59:34 +00:00
|
|
|
<img className="h-6 mx-auto" src="/calendso-logo-white-word.svg" alt="Cal.com Logo" />
|
2021-07-30 23:05:38 +00:00
|
|
|
<h2 className="mt-6 text-center text-3xl font-bold text-neutral-900">Sign in to your account</h2>
|
2021-06-24 15:59:11 +00:00
|
|
|
</div>
|
2021-04-30 21:14:26 +00:00
|
|
|
|
2021-06-24 15:59:11 +00:00
|
|
|
<div className="mt-8 sm:mx-auto sm:w-full sm:max-w-md">
|
2021-07-30 23:05:38 +00:00
|
|
|
<div className="bg-white py-8 px-4 mx-2 rounded-sm sm:px-10 border border-neutral-200">
|
2021-09-21 09:29:20 +00:00
|
|
|
<form className="space-y-6" onSubmit={handleSubmit}>
|
2021-06-24 15:59:11 +00:00
|
|
|
<input name="csrfToken" type="hidden" defaultValue={csrfToken} hidden />
|
|
|
|
<div>
|
2021-07-30 23:05:38 +00:00
|
|
|
<label htmlFor="email" className="block text-sm font-medium text-neutral-700">
|
2021-06-24 15:59:11 +00:00
|
|
|
Email address
|
|
|
|
</label>
|
|
|
|
<div className="mt-1">
|
|
|
|
<input
|
|
|
|
id="email"
|
|
|
|
name="email"
|
|
|
|
type="email"
|
|
|
|
autoComplete="email"
|
|
|
|
required
|
2021-09-21 09:29:20 +00:00
|
|
|
value={email}
|
|
|
|
onInput={(e) => setEmail(e.currentTarget.value)}
|
2021-07-30 23:05:38 +00:00
|
|
|
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"
|
2021-06-24 15:59:11 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-04-30 21:14:26 +00:00
|
|
|
|
2021-06-24 15:59:11 +00:00
|
|
|
<div>
|
2021-07-30 23:05:38 +00:00
|
|
|
<div className="flex">
|
|
|
|
<div className="w-1/2">
|
|
|
|
<label htmlFor="password" className="block text-sm font-medium text-neutral-700">
|
|
|
|
Password
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
<div className="w-1/2 text-right">
|
|
|
|
<Link href="/auth/forgot-password">
|
2021-08-03 08:07:39 +00:00
|
|
|
<a className="font-medium text-primary-600 text-sm">Forgot?</a>
|
2021-07-30 23:05:38 +00:00
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-06-24 15:59:11 +00:00
|
|
|
<div className="mt-1">
|
|
|
|
<input
|
|
|
|
id="password"
|
|
|
|
name="password"
|
|
|
|
type="password"
|
|
|
|
autoComplete="current-password"
|
|
|
|
required
|
2021-09-21 09:29:20 +00:00
|
|
|
value={password}
|
|
|
|
onInput={(e) => setPassword(e.currentTarget.value)}
|
2021-07-30 23:05:38 +00:00
|
|
|
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"
|
2021-06-24 15:59:11 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-04-30 21:14:26 +00:00
|
|
|
|
2021-09-21 09:29:20 +00:00
|
|
|
{secondFactorRequired && (
|
|
|
|
<div>
|
|
|
|
<label htmlFor="email" className="block text-sm font-medium text-neutral-700">
|
|
|
|
Two-Factor Code
|
|
|
|
</label>
|
|
|
|
<div className="mt-1">
|
|
|
|
<input
|
|
|
|
id="totpCode"
|
|
|
|
name="totpCode"
|
|
|
|
type="text"
|
|
|
|
maxLength={6}
|
|
|
|
minLength={6}
|
|
|
|
inputMode="numeric"
|
|
|
|
value={code}
|
|
|
|
onInput={(e) => 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"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
2021-06-24 15:59:11 +00:00
|
|
|
<div className="space-y-2">
|
|
|
|
<button
|
|
|
|
type="submit"
|
2021-09-21 09:29:20 +00:00
|
|
|
disabled={isSubmitting}
|
2021-08-02 15:24:01 +00:00
|
|
|
className="w-full flex justify-center py-2 px-4 border border-transparent rounded-sm shadow-sm text-sm font-medium text-white bg-neutral-900 hover:bg-neutral-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-black">
|
2021-06-24 15:59:11 +00:00
|
|
|
Sign in
|
|
|
|
</button>
|
2021-03-29 21:01:12 +00:00
|
|
|
</div>
|
2021-09-21 09:29:20 +00:00
|
|
|
|
|
|
|
{errorMessage && <p className="mt-1 text-sm text-red-700">{errorMessage}</p>}
|
2021-06-24 15:59:11 +00:00
|
|
|
</form>
|
2021-03-29 21:01:12 +00:00
|
|
|
</div>
|
2021-07-30 23:05:38 +00:00
|
|
|
<div className="mt-4 text-neutral-600 text-center text-sm">
|
2021-08-02 16:31:49 +00:00
|
|
|
Don't have an account? {/* replace this with your account creation flow */}
|
2021-09-15 18:18:16 +00:00
|
|
|
<a href="https://cal.com/signup" className="font-medium text-neutral-900">
|
2021-08-02 16:30:30 +00:00
|
|
|
Create an account
|
|
|
|
</a>
|
2021-07-30 23:05:38 +00:00
|
|
|
</div>
|
2021-06-24 15:59:11 +00:00
|
|
|
</div>
|
2021-04-30 21:14:26 +00:00
|
|
|
</div>
|
2021-06-24 15:59:11 +00:00
|
|
|
);
|
2021-03-29 21:01:12 +00:00
|
|
|
}
|
|
|
|
|
2021-08-09 10:35:06 +00:00
|
|
|
Login.getInitialProps = async (context) => {
|
|
|
|
const { req, res } = context;
|
|
|
|
const session = await getSession({ req });
|
|
|
|
|
|
|
|
if (session) {
|
|
|
|
res.writeHead(302, { Location: "/" });
|
|
|
|
res.end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-29 21:01:12 +00:00
|
|
|
return {
|
2021-08-09 10:35:06 +00:00
|
|
|
csrfToken: await getCsrfToken(context),
|
2021-06-24 15:59:11 +00:00
|
|
|
};
|
|
|
|
};
|