2022-02-04 20:30:36 +00:00
|
|
|
import { signIn } from "next-auth/react";
|
|
|
|
import { Dispatch, SetStateAction } from "react";
|
|
|
|
import { useFormContext } from "react-hook-form";
|
|
|
|
|
2022-07-22 17:27:06 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-03-16 23:36:43 +00:00
|
|
|
import Button from "@calcom/ui/Button";
|
|
|
|
|
2022-02-04 20:30:36 +00:00
|
|
|
import { useLocale } from "@lib/hooks/useLocale";
|
|
|
|
import { collectPageParameters, telemetryEventTypes, useTelemetry } from "@lib/telemetry";
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
email: string;
|
|
|
|
samlTenantID: string;
|
|
|
|
samlProductID: string;
|
|
|
|
hostedCal: boolean;
|
|
|
|
setErrorMessage: Dispatch<SetStateAction<string | null>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function SAMLLogin(props: Props) {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const methods = useFormContext();
|
|
|
|
const telemetry = useTelemetry();
|
|
|
|
|
2022-06-19 15:02:00 +00:00
|
|
|
const mutation = trpc.useMutation("viewer.public.samlTenantProduct", {
|
2022-02-04 20:30:36 +00:00
|
|
|
onSuccess: async (data) => {
|
|
|
|
await signIn("saml", {}, { tenant: data.tenant, product: data.product });
|
|
|
|
},
|
|
|
|
onError: (err) => {
|
|
|
|
props.setErrorMessage(err.message);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="mt-5">
|
|
|
|
<Button
|
|
|
|
color="secondary"
|
2022-07-12 17:50:04 +00:00
|
|
|
data-testid="saml"
|
2022-02-09 00:05:13 +00:00
|
|
|
className="flex w-full justify-center"
|
2022-02-04 20:30:36 +00:00
|
|
|
onClick={async (event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
// track Google logins. Without personal data/payload
|
2022-06-02 16:19:01 +00:00
|
|
|
telemetry.event(telemetryEventTypes.googleLogin, collectPageParameters());
|
2022-02-04 20:30:36 +00:00
|
|
|
|
|
|
|
if (!props.hostedCal) {
|
|
|
|
await signIn("saml", {}, { tenant: props.samlTenantID, product: props.samlProductID });
|
|
|
|
} else {
|
|
|
|
if (props.email.length === 0) {
|
|
|
|
props.setErrorMessage(t("saml_email_required"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// hosted solution, fetch tenant and product from the backend
|
|
|
|
mutation.mutate({
|
|
|
|
email: methods.getValues("email"),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}}>
|
|
|
|
{t("signin_with_saml")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|