cal.pub0.org/pages/auth/login.tsx

200 lines
7.2 KiB
TypeScript
Raw Normal View History

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.",
};
2021-03-29 21:01:12 +00:00
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<string | null>(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);
}
}
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">
<HeadSeo title="Login" description="Login" />
{isSubmitting && (
<div className="z-50 absolute w-full h-screen bg-gray-50 flex items-center">
<Loader />
</div>
)}
2021-06-24 15:59:11 +00:00
<div className="sm:mx-auto sm:w-full sm:max-w-md">
<img className="h-6 mx-auto" src="/calendso-logo-white-word.svg" alt="Cal.com Logo" />
<h2 className="font-cal 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-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">
<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"
inputMode="email"
2021-06-24 15:59:11 +00:00
autoComplete="email"
required
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-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">
<a tabIndex={-1} 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
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>
{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"
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>
{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&apos;t have an account? {/* replace this with your account creation flow */}
<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>
</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
};
};