refactor: move auth components and hooks from packages/features to apps/web/modules (#27223)
* refactor: move auth components and hooks from packages/features to apps/web/modules Co-Authored-By: [email protected] <[email protected]> * fix: add vitest import to Turnstile mock Co-Authored-By: [email protected] <[email protected]> * fix --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
9a927206bd
commit
7b8272ea20
12 files changed
+9
-16
No files matched your search
@@ -0,0 +1,80 @@
|
||||
import { signIn } from "next-auth/react";
|
||||
import type { Dispatch, SetStateAction } from "react";
|
||||
import { useFormContext } from "react-hook-form";
|
||||
import z from "zod";
|
||||
|
||||
import { LastUsed, useLastUsed } from "../hooks/useLastUsed";
|
||||
import { HOSTED_CAL_FEATURES } from "@calcom/lib/constants";
|
||||
import { emailRegex } from "@calcom/lib/emailSchema";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import type { ButtonProps } from "@calcom/ui/components/button";
|
||||
import { Button } from "@calcom/ui/components/button";
|
||||
|
||||
interface Props {
|
||||
samlTenantID: string;
|
||||
samlProductID: string;
|
||||
setErrorMessage: Dispatch<SetStateAction<string | null>>;
|
||||
}
|
||||
|
||||
const schema = z.object({
|
||||
email: z.string().regex(emailRegex, { message: "Please enter a valid email" }),
|
||||
});
|
||||
|
||||
export function SAMLLogin({
|
||||
samlTenantID,
|
||||
samlProductID,
|
||||
setErrorMessage,
|
||||
...buttonProps
|
||||
}: Props & ButtonProps) {
|
||||
const { t } = useLocale();
|
||||
const methods = useFormContext();
|
||||
const [lastUsed, setLastUsed] = useLastUsed();
|
||||
|
||||
const mutation = trpc.viewer.public.samlTenantProduct.useMutation({
|
||||
onSuccess: async (data) => {
|
||||
setLastUsed("saml");
|
||||
await signIn("saml", {}, { tenant: data.tenant, product: data.product });
|
||||
},
|
||||
onError: (err) => {
|
||||
setErrorMessage(t(err.message));
|
||||
},
|
||||
});
|
||||
|
||||
return (
|
||||
<Button
|
||||
StartIcon="lock"
|
||||
color="secondary"
|
||||
data-testid="samlAndOidc"
|
||||
className="flex w-full justify-center"
|
||||
onClick={async (event) => {
|
||||
event.preventDefault();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
mutation.mutate({
|
||||
email,
|
||||
});
|
||||
}}
|
||||
{...buttonProps}>
|
||||
<span>{t("signin_with_saml_oidc")}</span>
|
||||
{lastUsed === "saml" && <LastUsed />}
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import type { TurnstileProps } from "react-turnstile";
|
||||
import Turnstile from "react-turnstile";
|
||||
|
||||
import { CLOUDFLARE_SITE_ID } from "@calcom/lib/constants";
|
||||
|
||||
type Props = Omit<TurnstileProps, "sitekey">;
|
||||
|
||||
export default function TurnstileWidget(props: Props) {
|
||||
if (!CLOUDFLARE_SITE_ID || process.env.NEXT_PUBLIC_IS_E2E) return null;
|
||||
|
||||
return <Turnstile {...props} sitekey={CLOUDFLARE_SITE_ID} theme="auto" />;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { useState, useEffect } from "react";
|
||||
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { localStorage } from "@calcom/lib/webstorage";
|
||||
import classNames from "@calcom/ui/classNames";
|
||||
|
||||
type LoginType = "saml" | "google" | "credentials";
|
||||
|
||||
export function useLastUsed() {
|
||||
const [lastUsed, setLastUsed] = useState<LoginType>();
|
||||
|
||||
useEffect(() => {
|
||||
const storedValue = localStorage.getItem("last_cal_login");
|
||||
if (storedValue) {
|
||||
setLastUsed(storedValue as LoginType);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (lastUsed) {
|
||||
localStorage.setItem("last_cal_login", lastUsed);
|
||||
} else {
|
||||
localStorage.removeItem("last_cal_login");
|
||||
}
|
||||
}, [lastUsed]);
|
||||
|
||||
return [lastUsed, setLastUsed] as const;
|
||||
}
|
||||
|
||||
export const LastUsed = ({ className }: { className?: string }) => {
|
||||
const { t } = useLocale();
|
||||
return (
|
||||
<span className={classNames("text-subtle absolute right-3 text-xs", className)}>{t("last_used")}</span>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,30 @@
|
||||
"use client";
|
||||
|
||||
import { useSession } from "next-auth/react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useEffect } from "react";
|
||||
|
||||
import { WEBAPP_URL } from "@calcom/lib/constants";
|
||||
|
||||
export function useRedirectToLoginIfUnauthenticated(isPublic = false) {
|
||||
const { data: session, status } = useSession();
|
||||
const loading = status === "loading";
|
||||
const router = useRouter();
|
||||
useEffect(() => {
|
||||
if (isPublic) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!loading && !session) {
|
||||
const urlSearchParams = new URLSearchParams();
|
||||
urlSearchParams.set("callbackUrl", `${WEBAPP_URL}${location.pathname}${location.search}`);
|
||||
router.replace(`/auth/login?${urlSearchParams.toString()}`);
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [loading, session, isPublic]);
|
||||
|
||||
return {
|
||||
loading: loading && !session,
|
||||
session,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
"use client";
|
||||
|
||||
import { usePathname, useRouter } from "next/navigation";
|
||||
import { useEffect } from "react";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { useFlagMap } from "@calcom/features/flags/context/provider";
|
||||
import type { User } from "@calcom/prisma/client";
|
||||
import useMeQuery from "@calcom/trpc/react/hooks/useMeQuery";
|
||||
|
||||
const shouldShowOnboarding = (
|
||||
user: Pick<User, "createdDate" | "completedOnboarding"> & {
|
||||
organizationId: number | null;
|
||||
}
|
||||
) => {
|
||||
return (
|
||||
!user.completedOnboarding &&
|
||||
!user.organizationId &&
|
||||
dayjs(user.createdDate).isAfter(ONBOARDING_INTRODUCED_AT)
|
||||
);
|
||||
};
|
||||
|
||||
export const ONBOARDING_INTRODUCED_AT = dayjs("September 1 2021").toISOString();
|
||||
|
||||
export const ONBOARDING_NEXT_REDIRECT = {
|
||||
redirect: {
|
||||
permanent: false,
|
||||
destination: "/getting-started",
|
||||
},
|
||||
} as const;
|
||||
|
||||
export function useRedirectToOnboardingIfNeeded() {
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
const { data: user, isLoading } = useMeQuery();
|
||||
const flags = useFlagMap();
|
||||
|
||||
const needsEmailVerification =
|
||||
!user?.emailVerified && user?.identityProvider === "CAL" && flags["email-verification"];
|
||||
|
||||
const shouldRedirectToOnboarding = user && shouldShowOnboarding(user);
|
||||
// Don't redirect if already on an onboarding page (works for both old [[...step]] and v3 flows)
|
||||
const isOnOnboardingPage = pathname?.startsWith("/onboarding/") || pathname?.startsWith("/getting-started");
|
||||
const canRedirect =
|
||||
!isLoading && shouldRedirectToOnboarding && !needsEmailVerification && !isOnOnboardingPage;
|
||||
|
||||
useEffect(() => {
|
||||
if (canRedirect) {
|
||||
const gettingStartedPath = flags["onboarding-v3"] ? "/onboarding/getting-started" : "/getting-started";
|
||||
router.replace(gettingStartedPath);
|
||||
}
|
||||
}, [canRedirect, router, flags, pathname]);
|
||||
|
||||
return {
|
||||
isLoading,
|
||||
shouldRedirectToOnboarding,
|
||||
};
|
||||
}
|
||||
@@ -9,9 +9,9 @@ import { useEffect, useState } from "react";
|
||||
import { FormProvider, useForm } from "react-hook-form";
|
||||
import { z } from "zod";
|
||||
|
||||
import { SAMLLogin } from "@calcom/features/auth/SAMLLogin";
|
||||
import { SAMLLogin } from "@calcom/web/modules/auth/components/SAMLLogin";
|
||||
import { ErrorCode } from "@calcom/features/auth/lib/ErrorCode";
|
||||
import { LastUsed, useLastUsed } from "@calcom/features/auth/lib/hooks/useLastUsed";
|
||||
import { LastUsed, useLastUsed } from "@calcom/web/modules/auth/hooks/useLastUsed";
|
||||
import { HOSTED_CAL_FEATURES, WEBAPP_URL, WEBSITE_URL } from "@calcom/lib/constants";
|
||||
import { emailRegex } from "@calcom/lib/emailSchema";
|
||||
import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
|
||||
|
||||
Reference in new issue
Block a user