293 lines
11 KiB
TypeScript
293 lines
11 KiB
TypeScript
"use client";
|
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
import classNames from "classnames";
|
|
import { signIn } from "next-auth/react";
|
|
import Link from "next/link";
|
|
import { useRouter } from "next/navigation";
|
|
import { useEffect, useState } from "react";
|
|
import { FormProvider, useForm } from "react-hook-form";
|
|
import { z } from "zod";
|
|
|
|
import { SAMLLogin } from "@calcom/features/auth/SAMLLogin";
|
|
import { ErrorCode } from "@calcom/features/auth/lib/ErrorCode";
|
|
import { HOSTED_CAL_FEATURES, WEBAPP_URL, WEBSITE_URL } from "@calcom/lib/constants";
|
|
import { emailRegex } from "@calcom/lib/emailSchema";
|
|
import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
|
|
import { useCompatSearchParams } from "@calcom/lib/hooks/useCompatSearchParams";
|
|
import { useLastUsed, LastUsed } from "@calcom/lib/hooks/useLastUsed";
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
import { collectPageParameters, telemetryEventTypes, useTelemetry } from "@calcom/lib/telemetry";
|
|
import { trpc } from "@calcom/trpc/react";
|
|
import { Alert, Button, EmailField, PasswordField } from "@calcom/ui";
|
|
|
|
import type { inferSSRProps } from "@lib/types/inferSSRProps";
|
|
import type { WithNonceProps } from "@lib/withNonce";
|
|
|
|
import AddToHomescreen from "@components/AddToHomescreen";
|
|
import BackupCode from "@components/auth/BackupCode";
|
|
import TwoFactor from "@components/auth/TwoFactor";
|
|
import AuthContainer from "@components/ui/AuthContainer";
|
|
|
|
import type { getServerSideProps } from "@server/lib/auth/login/getServerSideProps";
|
|
|
|
interface LoginValues {
|
|
email: string;
|
|
password: string;
|
|
totpCode: string;
|
|
backupCode: string;
|
|
csrfToken: string;
|
|
}
|
|
|
|
const GoogleIcon = () => (
|
|
<img className="text-subtle mr-2 h-4 w-4" src="/google-icon-colored.svg" alt="Continue with Google Icon" />
|
|
);
|
|
export type PageProps = inferSSRProps<typeof getServerSideProps>;
|
|
export default function Login({
|
|
csrfToken,
|
|
isGoogleLoginEnabled,
|
|
isSAMLLoginEnabled,
|
|
samlTenantID,
|
|
samlProductID,
|
|
totpEmail,
|
|
}: // eslint-disable-next-line @typescript-eslint/ban-types
|
|
PageProps & WithNonceProps<{}>) {
|
|
const searchParams = useCompatSearchParams();
|
|
const { t } = useLocale();
|
|
const router = useRouter();
|
|
const formSchema = z
|
|
.object({
|
|
email: z
|
|
.string()
|
|
.min(1, `${t("error_required_field")}`)
|
|
.regex(emailRegex, `${t("enter_valid_email")}`),
|
|
...(!!totpEmail ? {} : { password: z.string().min(1, `${t("error_required_field")}`) }),
|
|
})
|
|
// Passthrough other fields like totpCode
|
|
.passthrough();
|
|
const methods = useForm<LoginValues>({ resolver: zodResolver(formSchema) });
|
|
const { register, formState } = methods;
|
|
const [twoFactorRequired, setTwoFactorRequired] = useState(!!totpEmail || false);
|
|
const [twoFactorLostAccess, setTwoFactorLostAccess] = useState(false);
|
|
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
|
const [lastUsed, setLastUsed] = useLastUsed();
|
|
|
|
const errorMessages: { [key: string]: string } = {
|
|
// [ErrorCode.SecondFactorRequired]: t("2fa_enabled_instructions"),
|
|
// Don't leak information about whether an email is registered or not
|
|
[ErrorCode.IncorrectEmailPassword]: t("incorrect_email_password"),
|
|
[ErrorCode.IncorrectTwoFactorCode]: `${t("incorrect_2fa_code")} ${t("please_try_again")}`,
|
|
[ErrorCode.InternalServerError]: `${t("something_went_wrong")} ${t("please_try_again_and_contact_us")}`,
|
|
[ErrorCode.ThirdPartyIdentityProviderEnabled]: t("account_created_with_identity_provider"),
|
|
};
|
|
|
|
const telemetry = useTelemetry();
|
|
|
|
let callbackUrl = searchParams?.get("callbackUrl") || "";
|
|
|
|
if (/"\//.test(callbackUrl)) callbackUrl = callbackUrl.substring(1);
|
|
|
|
// If not absolute URL, make it absolute
|
|
if (!/^https?:\/\//.test(callbackUrl)) {
|
|
callbackUrl = `${WEBAPP_URL}/${callbackUrl}`;
|
|
}
|
|
|
|
const safeCallbackUrl = getSafeRedirectUrl(callbackUrl);
|
|
|
|
callbackUrl = safeCallbackUrl || "";
|
|
|
|
const LoginFooter = (
|
|
<Link href={`${WEBSITE_URL}/signup`} className="text-brand-500 font-medium">
|
|
{t("dont_have_an_account")}
|
|
</Link>
|
|
);
|
|
|
|
const TwoFactorFooter = (
|
|
<>
|
|
<Button
|
|
onClick={() => {
|
|
if (twoFactorLostAccess) {
|
|
setTwoFactorLostAccess(false);
|
|
methods.setValue("backupCode", "");
|
|
} else {
|
|
setTwoFactorRequired(false);
|
|
methods.setValue("totpCode", "");
|
|
}
|
|
setErrorMessage(null);
|
|
}}
|
|
StartIcon="arrow-left"
|
|
color="minimal">
|
|
{t("go_back")}
|
|
</Button>
|
|
{!twoFactorLostAccess ? (
|
|
<Button
|
|
onClick={() => {
|
|
setTwoFactorLostAccess(true);
|
|
setErrorMessage(null);
|
|
methods.setValue("totpCode", "");
|
|
}}
|
|
StartIcon="lock"
|
|
color="minimal">
|
|
{t("lost_access")}
|
|
</Button>
|
|
) : null}
|
|
</>
|
|
);
|
|
|
|
const ExternalTotpFooter = (
|
|
<Button
|
|
onClick={() => {
|
|
window.location.replace("/");
|
|
}}
|
|
color="minimal">
|
|
{t("cancel")}
|
|
</Button>
|
|
);
|
|
|
|
const onSubmit = async (values: LoginValues) => {
|
|
setErrorMessage(null);
|
|
telemetry.event(telemetryEventTypes.login, collectPageParameters());
|
|
const res = await signIn<"credentials">("credentials", {
|
|
...values,
|
|
callbackUrl,
|
|
redirect: false,
|
|
});
|
|
if (!res) setErrorMessage(errorMessages[ErrorCode.InternalServerError]);
|
|
// we're logged in! let's do a hard refresh to the desired url
|
|
else if (!res.error) {
|
|
setLastUsed("credentials");
|
|
router.push(callbackUrl);
|
|
} else if (res.error === ErrorCode.SecondFactorRequired) setTwoFactorRequired(true);
|
|
else if (res.error === ErrorCode.IncorrectBackupCode) setErrorMessage(t("incorrect_backup_code"));
|
|
else if (res.error === ErrorCode.MissingBackupCodes) setErrorMessage(t("missing_backup_codes"));
|
|
// fallback if error not found
|
|
else setErrorMessage(errorMessages[res.error] || t("something_went_wrong"));
|
|
};
|
|
|
|
const { data, isPending, error } = trpc.viewer.public.ssoConnections.useQuery();
|
|
|
|
useEffect(
|
|
function refactorMeWithoutEffect() {
|
|
if (error) {
|
|
setErrorMessage(error.message);
|
|
}
|
|
},
|
|
[error]
|
|
);
|
|
|
|
const displaySSOLogin = HOSTED_CAL_FEATURES
|
|
? true
|
|
: isSAMLLoginEnabled && !isPending && data?.connectionExists;
|
|
|
|
return (
|
|
<div className="dark:bg-brand dark:text-brand-contrast text-emphasis min-h-screen [--cal-brand-emphasis:#101010] [--cal-brand-subtle:#9CA3AF] [--cal-brand-text:white] [--cal-brand:#111827] dark:[--cal-brand-emphasis:#e1e1e1] dark:[--cal-brand-text:black] dark:[--cal-brand:white]">
|
|
<AuthContainer
|
|
showLogo
|
|
heading={twoFactorRequired ? t("2fa_code") : t("welcome_back")}
|
|
footerText={
|
|
twoFactorRequired
|
|
? !totpEmail
|
|
? TwoFactorFooter
|
|
: ExternalTotpFooter
|
|
: process.env.NEXT_PUBLIC_DISABLE_SIGNUP !== "true"
|
|
? LoginFooter
|
|
: null
|
|
}>
|
|
<FormProvider {...methods}>
|
|
{!twoFactorRequired && (
|
|
<>
|
|
<div className="space-y-3">
|
|
{isGoogleLoginEnabled && (
|
|
<Button
|
|
color="primary"
|
|
className="w-full justify-center"
|
|
disabled={formState.isSubmitting}
|
|
data-testid="google"
|
|
CustomStartIcon={<GoogleIcon />}
|
|
onClick={async (e) => {
|
|
e.preventDefault();
|
|
setLastUsed("google");
|
|
await signIn("google", {
|
|
callbackUrl,
|
|
});
|
|
}}>
|
|
<span>{t("signin_with_google")}</span>
|
|
{lastUsed === "google" && <LastUsed />}
|
|
</Button>
|
|
)}
|
|
{displaySSOLogin && (
|
|
<SAMLLogin
|
|
disabled={formState.isSubmitting}
|
|
samlTenantID={samlTenantID}
|
|
samlProductID={samlProductID}
|
|
setErrorMessage={setErrorMessage}
|
|
/>
|
|
)}
|
|
</div>
|
|
{(isGoogleLoginEnabled || displaySSOLogin) && (
|
|
<div className="my-8">
|
|
<div className="relative flex items-center">
|
|
<div className="border-subtle flex-grow border-t" />
|
|
<span className="text-subtle mx-2 flex-shrink text-sm font-normal leading-none">
|
|
{t("or").toLocaleLowerCase()}
|
|
</span>
|
|
<div className="border-subtle flex-grow border-t" />
|
|
</div>
|
|
</div>
|
|
)}
|
|
</>
|
|
)}
|
|
|
|
<form onSubmit={methods.handleSubmit(onSubmit)} noValidate data-testid="login-form">
|
|
<div>
|
|
<input defaultValue={csrfToken || undefined} type="hidden" hidden {...register("csrfToken")} />
|
|
</div>
|
|
<div className="space-y-6">
|
|
<div className={classNames("space-y-6", { hidden: twoFactorRequired })}>
|
|
<EmailField
|
|
id="email"
|
|
label={t("email_address")}
|
|
defaultValue={totpEmail || (searchParams?.get("email") as string)}
|
|
placeholder="[email protected]"
|
|
required
|
|
{...register("email")}
|
|
/>
|
|
<div className="relative">
|
|
<PasswordField
|
|
id="password"
|
|
autoComplete="off"
|
|
required={!totpEmail}
|
|
className="mb-0"
|
|
{...register("password")}
|
|
/>
|
|
<div className="absolute -top-[2px] ltr:right-0 rtl:left-0">
|
|
<Link
|
|
href="/auth/forgot-password"
|
|
tabIndex={-1}
|
|
className="text-default text-sm font-medium">
|
|
{t("forgot")}
|
|
</Link>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{twoFactorRequired ? !twoFactorLostAccess ? <TwoFactor center /> : <BackupCode center /> : null}
|
|
|
|
{errorMessage && <Alert severity="error" title={errorMessage} />}
|
|
<Button
|
|
type="submit"
|
|
color="secondary"
|
|
disabled={formState.isSubmitting}
|
|
className="w-full justify-center">
|
|
<span>{twoFactorRequired ? t("submit") : t("sign_in")}</span>
|
|
{lastUsed === "credentials" && !twoFactorRequired && <LastUsed className="text-gray-600" />}
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
</FormProvider>
|
|
</AuthContainer>
|
|
<AddToHomescreen />
|
|
</div>
|
|
);
|
|
}
|