From 22380a6c46c22ee0c4373b7ff454906c1e93b59f Mon Sep 17 00:00:00 2001 From: panteliselef Date: Wed, 19 Jun 2024 21:06:36 +0300 Subject: [PATCH 1/2] fix(clerk-js): Improve solution for saml and hidden password field introduced in #3578 --- .changeset/twenty-maps-train.md | 2 ++ .../src/ui/components/SignIn/SignInStart.tsx | 32 ++++++++++++------- 2 files changed, 23 insertions(+), 11 deletions(-) create mode 100644 .changeset/twenty-maps-train.md diff --git a/.changeset/twenty-maps-train.md b/.changeset/twenty-maps-train.md new file mode 100644 index 00000000000..a845151cc84 --- /dev/null +++ b/.changeset/twenty-maps-train.md @@ -0,0 +1,2 @@ +--- +--- diff --git a/packages/clerk-js/src/ui/components/SignIn/SignInStart.tsx b/packages/clerk-js/src/ui/components/SignIn/SignInStart.tsx index 5e325281cc4..cdc4a70e962 100644 --- a/packages/clerk-js/src/ui/components/SignIn/SignInStart.tsx +++ b/packages/clerk-js/src/ui/components/SignIn/SignInStart.tsx @@ -1,6 +1,6 @@ import { useClerk } from '@clerk/shared/react'; import { isWebAuthnAutofillSupported, isWebAuthnSupported } from '@clerk/shared/webauthn'; -import type { ClerkAPIError, SignInCreateParams } from '@clerk/types'; +import type { ClerkAPIError, SignInCreateParams, SignInResource } from '@clerk/types'; import { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'; import { ERROR_CODES } from '../../../core/constants'; @@ -245,18 +245,28 @@ export function _SignInStart(): JSX.Element { } as SignInCreateParams; }; + const safePasswordSignInForSamlInstance = ( + signInCreatePromise: Promise, + fields: Array>, + ) => { + return signInCreatePromise.then(signInResource => { + if (!userSettings.saml.enabled) { + return signInResource; + } + /** + * For SAML enabled instances, perform sign in with password only when it is allowed for the identified user. + */ + const passwordField = fields.find(f => f.name === 'password')?.value; + if (!passwordField || signInResource.supportedFirstFactors.some(ff => ff.strategy === 'saml')) { + return signInResource; + } + return signInResource.attemptFirstFactor({ strategy: 'password', password: passwordField }); + }); + }; + const signInWithFields = async (...fields: Array>) => { try { - const res = await signIn.create(buildSignInParams(fields)).then(signInResource => { - /** - * For SAML enabled instances, perform sign in with password only when it is allowed for the identified user. - */ - const passwordField = fields.find(f => f.name === 'password')?.value; - if (!passwordField || signInResource.supportedFirstFactors.some(ff => ff.strategy === 'saml')) { - return signInResource; - } - return signInResource.attemptFirstFactor({ strategy: 'password', password: passwordField }); - }); + const res = await safePasswordSignInForSamlInstance(signIn.create(buildSignInParams(fields)), fields); switch (res.status) { case 'needs_identifier': From ff03ba907062276a569ec35c72a94fd8b6ec0c5c Mon Sep 17 00:00:00 2001 From: panteliselef Date: Wed, 19 Jun 2024 22:04:00 +0300 Subject: [PATCH 2/2] fix(clerk-js): Improve solution for saml and hidden password field introduced in #3578 --- packages/clerk-js/src/ui/utils/test/fixtures.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/clerk-js/src/ui/utils/test/fixtures.ts b/packages/clerk-js/src/ui/utils/test/fixtures.ts index d32051038b7..d7291bc825d 100644 --- a/packages/clerk-js/src/ui/utils/test/fixtures.ts +++ b/packages/clerk-js/src/ui/utils/test/fixtures.ts @@ -178,6 +178,7 @@ const createBaseUserSettings = (): UserSettingsJSON => { attributes: { ...attributeConfig }, actions: { delete_self: false, create_organization: false }, social: { ...socialConfig }, + saml: { enabled: false }, sign_in: { second_factor: { required: false,