2022-08-24 20:18:42 +00:00
|
|
|
import { signIn } from "next-auth/react";
|
|
|
|
import { Dispatch, SetStateAction } from "react";
|
|
|
|
import { useFormContext } from "react-hook-form";
|
2022-10-19 15:50:25 +00:00
|
|
|
import z from "zod";
|
2022-08-24 20:18:42 +00:00
|
|
|
|
2022-10-19 15:50:25 +00:00
|
|
|
import { HOSTED_CAL_FEATURES } from "@calcom/lib/constants";
|
2022-08-24 20:18:42 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2023-01-23 23:08:01 +00:00
|
|
|
import { Button } from "@calcom/ui";
|
|
|
|
import { FiLock } from "@calcom/ui/components/icon";
|
2022-08-24 20:18:42 +00:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
samlTenantID: string;
|
|
|
|
samlProductID: string;
|
|
|
|
setErrorMessage: Dispatch<SetStateAction<string | null>>;
|
|
|
|
}
|
|
|
|
|
2022-10-19 15:50:25 +00:00
|
|
|
const schema = z.object({
|
|
|
|
email: z.string().email({ message: "Please enter a valid email" }),
|
|
|
|
});
|
|
|
|
|
2022-12-22 19:06:26 +00:00
|
|
|
export function SAMLLogin({ samlTenantID, samlProductID, setErrorMessage }: Props) {
|
2022-08-24 20:18:42 +00:00
|
|
|
const { t } = useLocale();
|
|
|
|
const methods = useFormContext();
|
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const mutation = trpc.viewer.public.samlTenantProduct.useMutation({
|
2022-08-24 20:18:42 +00:00
|
|
|
onSuccess: async (data) => {
|
|
|
|
await signIn("saml", {}, { tenant: data.tenant, product: data.product });
|
|
|
|
},
|
|
|
|
onError: (err) => {
|
2022-10-19 15:50:25 +00:00
|
|
|
setErrorMessage(t(err.message));
|
2022-08-24 20:18:42 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
2022-09-13 16:35:14 +00:00
|
|
|
<Button
|
2023-01-23 23:08:01 +00:00
|
|
|
StartIcon={FiLock}
|
2022-09-13 16:35:14 +00:00
|
|
|
color="secondary"
|
|
|
|
data-testid="saml"
|
|
|
|
className="flex w-full justify-center"
|
|
|
|
onClick={async (event) => {
|
|
|
|
event.preventDefault();
|
2022-08-24 20:18:42 +00:00
|
|
|
|
2022-10-19 15:50:25 +00:00
|
|
|
if (!HOSTED_CAL_FEATURES) {
|
|
|
|
await signIn("saml", {}, { tenant: samlTenantID, product: samlProductID });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hosted solution, fetch tenant and product from the backend
|
|
|
|
const email = methods.getValues("email");
|
|
|
|
const parsed = schema.safeParse({ email });
|
|
|
|
|
|
|
|
if (!parsed.success) {
|
|
|
|
const {
|
|
|
|
fieldErrors: { email },
|
|
|
|
} = parsed.error.flatten();
|
|
|
|
|
|
|
|
setErrorMessage(email ? email[0] : null);
|
|
|
|
return;
|
2022-09-13 16:35:14 +00:00
|
|
|
}
|
2022-10-19 15:50:25 +00:00
|
|
|
|
|
|
|
mutation.mutate({
|
|
|
|
email,
|
|
|
|
});
|
2022-09-13 16:35:14 +00:00
|
|
|
}}>
|
|
|
|
{t("signin_with_saml")}
|
|
|
|
</Button>
|
2022-08-24 20:18:42 +00:00
|
|
|
);
|
|
|
|
}
|