{"version":3,"file":"usePasswordlessAuthenticate.mjs","sources":["../../../../../../../../../../web/src/ui/b2c/screens/Secondary/usePasswordlessAuthenticate.ts"],"sourcesContent":["import { useLingui } from '@lingui/react/macro';\nimport { DEFAULT_OTP_EXPIRATION_MINUTES } from '@stytch/core';\nimport { OTPMethods, StytchEventType, StytchSDKAPIError } from '@stytch/core/public';\nimport { useState } from 'react';\n\nimport { convertMagicLinkOptions } from '../../../../utils';\nimport { getTranslatedError } from '../../../../utils/getTranslatedError';\nimport { errorToast } from '../../../components/atoms/Toast';\nimport {\n  useConfig,\n  useErrorCallback,\n  useEventCallback,\n  useGlobalReducer,\n  useStytch,\n} from '../../GlobalContextProvider';\n\nexport function usePasswordlessAuthenticate() {\n  const { t } = useLingui();\n\n  const stytchClient = useStytch();\n  const { emailMagicLinksOptions, otpOptions } = useConfig();\n  const [state, dispatch] = useGlobalReducer();\n  const onEvent = useEventCallback();\n  const onError = useErrorCallback();\n\n  const [isSubmitting, setIsSubmitting] = useState(false);\n\n  const email =\n    state.formState.magicLinkState.email !== ''\n      ? state.formState.magicLinkState.email\n      : state.formState.passwordState.email;\n\n  const sendLink = async () => {\n    setIsSubmitting(true);\n\n    try {\n      const data = await stytchClient.magicLinks.email.loginOrCreate(\n        email,\n        convertMagicLinkOptions(emailMagicLinksOptions),\n      );\n      onEvent({ type: StytchEventType.MagicLinkLoginOrCreateEvent, data: { ...data, email } });\n      dispatch({ type: 'set_magic_link_email', email: email });\n    } catch (e) {\n      onError(e as StytchSDKAPIError);\n      errorToast({ message: getTranslatedError(e as StytchSDKAPIError, t) });\n      throw e;\n    } finally {\n      setIsSubmitting(false);\n    }\n  };\n\n  const sendCode = async () => {\n    setIsSubmitting(true);\n\n    try {\n      const data = await stytchClient.otps.email.loginOrCreate(email, {\n        expiration_minutes: otpOptions?.expirationMinutes ?? DEFAULT_OTP_EXPIRATION_MINUTES,\n        locale: otpOptions?.locale,\n      });\n      onEvent({ type: StytchEventType.OTPsLoginOrCreateEvent, data });\n      dispatch({\n        type: 'update_otp_state',\n        otpState: {\n          type: OTPMethods.Email,\n          methodId: data.method_id,\n          otpDestination: email,\n        },\n      });\n    } catch (e) {\n      onError(e as StytchSDKAPIError);\n      errorToast({ message: getTranslatedError(e as StytchSDKAPIError, t) });\n      throw e;\n    } finally {\n      setIsSubmitting(false);\n    }\n  };\n\n  return {\n    sendLink,\n    sendCode,\n    isSubmitting,\n  };\n}\n"],"names":["usePasswordlessAuthenticate","useLingui","stytchClient","useStytch","emailMagicLinksOptions","otpOptions","useConfig","state","dispatch","useGlobalReducer","onEvent","useEventCallback","onError","useErrorCallback","isSubmitting","setIsSubmitting","useState","email","formState","magicLinkState","passwordState","sendLink","data","magicLinks","loginOrCreate","convertMagicLinkOptions","type","StytchEventType","MagicLinkLoginOrCreateEvent","e","errorToast","message","getTranslatedError","sendCode","otps","expiration_minutes","expirationMinutes","DEFAULT_OTP_EXPIRATION_MINUTES","locale","OTPsLoginOrCreateEvent","otpState","OTPMethods","Email","methodId","method_id","otpDestination"],"mappings":";;;;;;;;;;AAgBO,SAASA,2BAAAA,GAAAA;AACd,IAAA,MAAM,EAAA,IAAA,EAAA,OAAA,EAAA,CAAA,EAAA,GAAA,EAAA,GAAQC,SAAAA,EAAAA;AAEd,IAAA,MAAMC,YAAAA,GAAeC,SAAAA,EAAAA;AACrB,IAAA,MAAM,EAAEC,sBAAsB,EAAEC,UAAU,EAAE,GAAGC,SAAAA,EAAAA;IAC/C,MAAM,CAACC,KAAAA,EAAOC,QAAAA,CAAS,GAAGC,gBAAAA,EAAAA;AAC1B,IAAA,MAAMC,OAAAA,GAAUC,gBAAAA,EAAAA;AAChB,IAAA,MAAMC,OAAAA,GAAUC,gBAAAA,EAAAA;AAEhB,IAAA,MAAM,CAACC,YAAAA,EAAcC,eAAAA,CAAgB,GAAGC,CAAAA,CAAS,KAAA,CAAA;IAEjD,MAAMC,KAAAA,GACJV,MAAMW,SAAS,CAACC,cAAc,CAACF,KAAK,KAAK,EAAA,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