fix: fixes oauth flow with user sign up (#27307)
* fix oauth flow with sign up * extract key as const variable --------- Co-authored-by: CarinaWolli <[email protected]>
This commit is contained in:
1 parent
5bea18fd5e
commit
8e5a5629a4
2 files changed
+101
-32
No files matched your search
@@ -11,8 +11,15 @@ import { z } from "zod";
|
||||
|
||||
import { SAMLLogin } from "@calcom/web/modules/auth/components/SAMLLogin";
|
||||
import { ErrorCode } from "@calcom/features/auth/lib/ErrorCode";
|
||||
import { LastUsed, useLastUsed } from "@calcom/web/modules/auth/hooks/useLastUsed";
|
||||
import { HOSTED_CAL_FEATURES, WEBAPP_URL, WEBSITE_URL } from "@calcom/lib/constants";
|
||||
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";
|
||||
import { useCompatSearchParams } from "@calcom/lib/hooks/useCompatSearchParams";
|
||||
@@ -40,7 +47,11 @@ interface LoginValues {
|
||||
}
|
||||
|
||||
const GoogleIcon = () => (
|
||||
<img className="text-subtle mr-2 h-4 w-4" src="/google-icon-colored.svg" alt="Continue with Google Icon" />
|
||||
<img
|
||||
className="mr-2 w-4 h-4 text-subtle"
|
||||
src="/google-icon-colored.svg"
|
||||
alt="Continue with Google Icon"
|
||||
/>
|
||||
);
|
||||
export type PageProps = inferSSRProps<typeof getServerSideProps>;
|
||||
export default function Login({
|
||||
@@ -60,13 +71,17 @@ export default function Login({
|
||||
.string()
|
||||
.min(1, `${t("error_required_field")}`)
|
||||
.regex(emailRegex, `${t("enter_valid_email")}`),
|
||||
...(totpEmail ? {} : { password: z.string().min(1, `${t("error_required_field")}`) }),
|
||||
...(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 [twoFactorRequired, setTwoFactorRequired] = useState(
|
||||
!!totpEmail || false
|
||||
);
|
||||
const [twoFactorLostAccess, setTwoFactorLostAccess] = useState(false);
|
||||
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
||||
const [lastUsed, setLastUsed] = useLastUsed();
|
||||
@@ -75,9 +90,15 @@ export default function Login({
|
||||
// [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"),
|
||||
[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"
|
||||
),
|
||||
};
|
||||
|
||||
let callbackUrl = searchParams?.get("callbackUrl") || "";
|
||||
@@ -93,8 +114,12 @@ export default function Login({
|
||||
|
||||
callbackUrl = safeCallbackUrl || "";
|
||||
|
||||
const signupUrl = callbackUrl
|
||||
? `${WEBSITE_URL}/signup?redirect=${encodeURIComponent(callbackUrl)}`
|
||||
: `${WEBSITE_URL}/signup`;
|
||||
|
||||
const LoginFooter = (
|
||||
<Link href={`${WEBSITE_URL}/signup`} className="text-brand-500 font-medium">
|
||||
<Link href={signupUrl} className="font-medium text-brand-500">
|
||||
{t("dont_have_an_account")}
|
||||
</Link>
|
||||
);
|
||||
@@ -113,7 +138,8 @@ export default function Login({
|
||||
setErrorMessage(null);
|
||||
}}
|
||||
StartIcon="arrow-left"
|
||||
color="minimal">
|
||||
color="minimal"
|
||||
>
|
||||
{t("go_back")}
|
||||
</Button>
|
||||
{!twoFactorLostAccess ? (
|
||||
@@ -124,7 +150,8 @@ export default function Login({
|
||||
methods.setValue("totpCode", "");
|
||||
}}
|
||||
StartIcon="lock"
|
||||
color="minimal">
|
||||
color="minimal"
|
||||
>
|
||||
{t("lost_access")}
|
||||
</Button>
|
||||
) : null}
|
||||
@@ -136,7 +163,8 @@ export default function Login({
|
||||
onClick={() => {
|
||||
window.location.replace("/");
|
||||
}}
|
||||
color="minimal">
|
||||
color="minimal"
|
||||
>
|
||||
{t("cancel")}
|
||||
</Button>
|
||||
);
|
||||
@@ -154,14 +182,18 @@ export default function Login({
|
||||
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"));
|
||||
} 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();
|
||||
const { data, isPending, error } =
|
||||
trpc.viewer.public.ssoConnections.useQuery();
|
||||
|
||||
useEffect(
|
||||
function refactorMeWithoutEffect() {
|
||||
@@ -186,10 +218,12 @@ export default function Login({
|
||||
? !totpEmail
|
||||
? TwoFactorFooter
|
||||
: ExternalTotpFooter
|
||||
: process.env.NEXT_PUBLIC_DISABLE_SIGNUP !== "true" && searchParams?.get("register") !== "false"
|
||||
: process.env.NEXT_PUBLIC_DISABLE_SIGNUP !== "true" &&
|
||||
searchParams?.get("register") !== "false"
|
||||
? LoginFooter
|
||||
: null
|
||||
}>
|
||||
}
|
||||
>
|
||||
<FormProvider {...methods}>
|
||||
{!twoFactorRequired && (
|
||||
<>
|
||||
@@ -197,7 +231,7 @@ export default function Login({
|
||||
{isGoogleLoginEnabled && (
|
||||
<Button
|
||||
color="primary"
|
||||
className="w-full justify-center"
|
||||
className="justify-center w-full"
|
||||
disabled={formState.isSubmitting}
|
||||
data-testid="google"
|
||||
CustomStartIcon={<GoogleIcon />}
|
||||
@@ -207,7 +241,8 @@ export default function Login({
|
||||
await signIn("google", {
|
||||
callbackUrl,
|
||||
});
|
||||
}}>
|
||||
}}
|
||||
>
|
||||
<span>{t("signin_with_google")}</span>
|
||||
{lastUsed === "google" && <LastUsed />}
|
||||
</Button>
|
||||
@@ -223,28 +258,43 @@ export default function Login({
|
||||
</div>
|
||||
{(isGoogleLoginEnabled || displaySSOLogin) && (
|
||||
<div className="my-8">
|
||||
<div className="relative flex items-center">
|
||||
<div className="border-subtle grow border-t" />
|
||||
<span className="text-subtle mx-2 shrink text-sm font-normal leading-none">
|
||||
<div className="flex relative items-center">
|
||||
<div className="border-t border-subtle grow" />
|
||||
<span className="mx-2 text-sm font-normal leading-none text-subtle shrink">
|
||||
{t("or").toLocaleLowerCase()}
|
||||
</span>
|
||||
<div className="border-subtle grow border-t" />
|
||||
<div className="border-t border-subtle grow" />
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
|
||||
<form onSubmit={methods.handleSubmit(onSubmit)} noValidate data-testid="login-form">
|
||||
<form
|
||||
onSubmit={methods.handleSubmit(onSubmit)}
|
||||
noValidate
|
||||
data-testid="login-form"
|
||||
>
|
||||
<div>
|
||||
<input defaultValue={csrfToken || undefined} type="hidden" hidden {...register("csrfToken")} />
|
||||
<input
|
||||
defaultValue={csrfToken || undefined}
|
||||
type="hidden"
|
||||
hidden
|
||||
{...register("csrfToken")}
|
||||
/>
|
||||
</div>
|
||||
<div className="stack-y-6">
|
||||
<div className={classNames("stack-y-6", { hidden: twoFactorRequired })}>
|
||||
<div
|
||||
className={classNames("stack-y-6", {
|
||||
hidden: twoFactorRequired,
|
||||
})}
|
||||
>
|
||||
<EmailField
|
||||
id="email"
|
||||
label={t("email_address")}
|
||||
defaultValue={totpEmail || (searchParams?.get("email") as string)}
|
||||
defaultValue={
|
||||
totpEmail || (searchParams?.get("email") as string)
|
||||
}
|
||||
placeholder="[email protected]"
|
||||
required
|
||||
autoComplete="email"
|
||||
@@ -262,23 +312,33 @@ export default function Login({
|
||||
<Link
|
||||
href="/auth/forgot-password"
|
||||
tabIndex={-1}
|
||||
className="text-default text-sm font-medium">
|
||||
className="text-sm font-medium text-default"
|
||||
>
|
||||
{t("forgot")}
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{twoFactorRequired ? !twoFactorLostAccess ? <TwoFactor center /> : <BackupCode center /> : null}
|
||||
{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">
|
||||
className="justify-center w-full"
|
||||
>
|
||||
<span>{twoFactorRequired ? t("submit") : t("sign_in")}</span>
|
||||
{lastUsed === "credentials" && !twoFactorRequired && <LastUsed className="text-gray-600" />}
|
||||
{lastUsed === "credentials" && !twoFactorRequired && (
|
||||
<LastUsed className="text-gray-600" />
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
@@ -6,6 +6,7 @@ import { trpc } from "@calcom/trpc/react";
|
||||
import { showToast } from "@calcom/ui/components/toast";
|
||||
import { setShowWelcomeToCalcomModalFlag } from "@calcom/web/modules/shell/hooks/useWelcomeToCalcomModal";
|
||||
|
||||
const ONBOARDING_REDIRECT_KEY = "onBoardingRedirect";
|
||||
const ORG_MODAL_STORAGE_KEY = "showNewOrgModal";
|
||||
|
||||
const DEFAULT_EVENT_TYPES = [
|
||||
@@ -56,6 +57,14 @@ export const useSubmitPersonalOnboarding = () => {
|
||||
}
|
||||
|
||||
await utils.viewer.me.get.refetch();
|
||||
|
||||
const redirectUrl = localStorage.getItem(ONBOARDING_REDIRECT_KEY);
|
||||
if (redirectUrl) {
|
||||
localStorage.removeItem(ONBOARDING_REDIRECT_KEY);
|
||||
router.push(redirectUrl);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if org modal flag is set - if so, don't show personal modal
|
||||
// Organization onboarding takes precedence
|
||||
const hasOrgModalFlag = sessionStorage.getItem(ORG_MODAL_STORAGE_KEY) === "true";
|
||||
|
||||
Reference in new issue
Block a user