@privy-io/react-auth
Version:
React client for the Privy Auth API
2 lines (1 loc) • 12.6 kB
JavaScript
"use strict";var e=require("react/jsx-runtime"),r=require("@heroicons/react/24/outline/QuestionMarkCircleIcon"),o=require("@heroicons/react/24/outline/ShieldCheckIcon"),n=require("@heroicons/react/24/solid/CheckBadgeIcon"),t=require("@heroicons/react/24/solid/IdentificationIcon"),i=require("react"),s=require("./ModalHeader-BCPQgekO.js"),l=require("./useActiveWallet-Cn6zOXx5.js"),a=require("./context-D-ZJYjUg.js"),c=require("./internal-context-BJv4f_bO.js"),u=require("./get-is-unified-wallet-UpdjHxOV.js"),d=require("./index-C8lpxYDX.js"),h=require("./EnrollTotp-B1qE1mCx.js"),m=require("@heroicons/react/24/outline/CheckCircleIcon"),j=require("@heroicons/react/24/outline/PhoneIcon"),x=require("@privy-io/js-sdk-core"),y=require("./ConnectPhoneForm-BPgbe0j3.js"),f=require("./PinInput-B12fA7pp.js"),p=require("./ErrorScreen-BPXQabSS.js");function q(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}require("styled-components"),require("@heroicons/react/24/outline/ArrowLeftIcon"),require("@heroicons/react/24/outline/ArrowRightIcon"),require("@heroicons/react/24/outline/XMarkIcon"),require("zustand"),require("react-device-detect"),require("./use-export-wallet-B5ujV5Nk.js"),require("eventemitter3"),require("viem/utils"),require("./useWallets-CKAypPZB.js"),require("./events-context-Di6--rDg.js"),require("viem"),require("./getPublicClient-BsmZyCGX.js"),require("tinycolor2"),require("ofetch"),require("@privy-io/are-addresses-equal"),require("uuid"),require("jose"),require("@coinbase/wallet-sdk"),require("@privy-io/ethereum"),require("mipd"),require("@privy-io/popup"),require("./paths-DizMb-lU.js"),require("./usePrivy-cfS6o4tX.js"),require("@scure/base"),require("@headlessui/react"),require("@walletconnect/ethereum-provider"),require("@privy-io/urls"),require("./PrivyPluginContext-DsgaS6n9.js"),require("./getEmbeddedConnectedWallet-pJd-l936.js"),require("js-cookie"),require("./frame-Oz7volks.js"),require("@privy-io/routes"),require("x402/client"),require("@privy-io/api-base"),require("viem/accounts"),require("./use-sign-with-user-signer-Cu_6Bx7m.js"),require("@heroicons/react/24/outline/ChevronRightIcon"),require("@heroicons/react/24/outline/DevicePhoneMobileIcon"),require("@heroicons/react/24/outline/FingerPrintIcon"),require("@heroicons/react/24/outline/MinusCircleIcon"),require("./Chip-C6r7fZiR.js"),require("./LoadingSkeleton-BoyADunQ.js"),require("@heroicons/react/24/outline/ArrowRightEndOnRectangleIcon"),require("@heroicons/react/24/outline/ClockIcon"),require("./LinkPasskeyScreen-CjdxFDoA.js"),require("lucide-react"),require("./TodoList-Cii_KrAk.js"),require("./ScreenLayout-CMb-Fua0.js"),require("./Screen-BfKedqka.js"),require("./index-ByFhjjzH.js"),require("./CopyToClipboard-CC0DCGEH.js"),require("./Layouts-D3TOcPhm.js"),require("./Column-D8_5wTPp.js"),require("./LabelXs-C8rLnCfU.js"),require("./Subtitle-CR8zM7ZJ.js"),require("./Title-ifTy35V5.js"),require("./shared-DBm7PUrg.js"),require("@heroicons/react/24/solid/ShieldCheckIcon"),require("./QrCode-CE12LRBB.js"),require("qrcode"),require("./reservoir-Bx0HQwbb.js");var v=/*#__PURE__*/q(r),C=/*#__PURE__*/q(o),M=/*#__PURE__*/q(n),g=/*#__PURE__*/q(t),S=/*#__PURE__*/q(m),w=/*#__PURE__*/q(j);const k=({appName:r,onComplete:o,onReset:n,onClose:t})=>{let[l,c]=i.useState(""),[h,m]=i.useState(!1),[j,q]=i.useState(null),[v,C]=i.useState("enroll"),{initEnrollmentWithSms:M,submitEnrollmentWithSms:g}=d.useMfaEnrollment(),{data:k}=u.usePrivyModal(),b=a.useAppConfig();function I(){k?.mfaEnrollmentFlow?.onSuccess(),o()}return j?/*#__PURE__*/e.jsx(p.ErrorScreenView,{allowlistConfig:b.allowlistConfig,error:j,onBack:()=>q(null),onRetry:()=>q(null)}):"enroll"===v?/*#__PURE__*/e.jsxs(e.Fragment,{children:[/*#__PURE__*/e.jsx(s.ModalHeader,{backFn:n,onClose:t},"header"),/*#__PURE__*/e.jsx(f.IconWrapper,{style:{marginBottom:"1.5rem"},children:/*#__PURE__*/e.jsx(w.default,{})}),/*#__PURE__*/e.jsx(f.Title,{children:"Set up SMS verification"}),/*#__PURE__*/e.jsxs(f.SubTitle,{children:["We'll text a verification code to this mobile device whenever you use your ",r," ","wallet."]}),/*#__PURE__*/e.jsxs(f.Container,{children:[/*#__PURE__*/e.jsx(y.ConnectPhoneNumberForm,{onSubmit:async function({qualifiedPhoneNumber:e}){try{await M({phoneNumber:e}),c(e),C("verify")}catch(e){q(e)}},hideRecent:!0}),/*#__PURE__*/e.jsxs(f.TermsText,{children:["By providing your mobile number, you agree to receive text messages from ",b?.name,". Some carrier charges may apply"]})]}),/*#__PURE__*/e.jsx(s.ModalFooter,{})]}):h?/*#__PURE__*/e.jsxs(e.Fragment,{children:[/*#__PURE__*/e.jsx(s.ModalHeader,{onClose:I},"header"),/*#__PURE__*/e.jsx(f.IconWrapper,{style:{marginBottom:"1.5rem"},children:/*#__PURE__*/e.jsx(S.default,{})}),/*#__PURE__*/e.jsx(f.Title,{children:"SMS verification added"}),/*#__PURE__*/e.jsxs(f.SubTitle,{children:["From now on, you'll enter the verification code sent to your mobile device whenever you use your ",r," wallet."]}),/*#__PURE__*/e.jsx(f.BottomSection,{children:/*#__PURE__*/e.jsx(s.PrimaryButton,{onClick:I,children:"Done"})}),/*#__PURE__*/e.jsx(s.ModalFooter,{})]}):/*#__PURE__*/e.jsxs(e.Fragment,{children:[/*#__PURE__*/e.jsx(s.ModalHeader,{backFn:function(){"verify"===v?C("enroll"):n()},onClose:t},"header"),/*#__PURE__*/e.jsx(f.IconWrapper,{style:{marginBottom:"1.5rem"},children:/*#__PURE__*/e.jsx(w.default,{})}),/*#__PURE__*/e.jsx(f.Title,{children:"Enter enrollment code"}),/*#__PURE__*/e.jsxs(f.Container,{children:[/*#__PURE__*/e.jsx(f.NumericPinInput,{onChange:async function(e){try{if(!e)return;await g({phoneNumber:l,mfaCode:e}),m(!0)}catch(e){if(d.errorIndicatesMfaRateLimit(e))throw Error("You have exceeded the maximum number of attempts. Please close this window and try again in 10 seconds.");if(d.errorIndicatesMfaVerificationFailed(e))throw Error("The code you entered is not valid");if(d.errorIndicatesMfaTimeout(e))throw Error("You have exceeded the time limit for code entry. Please try again in 30 seconds.");throw d.errorIndicatesMfaCanceled(e)?Error("Verification canceled"):Error("Unknown error")}}}),/*#__PURE__*/e.jsxs(f.SubTitle,{children:["To continue, enter the 6-digit code sent to ",/*#__PURE__*/e.jsx("strong",{children:x.lastFourDigits(l)})]})]}),/*#__PURE__*/e.jsx(s.ModalFooter,{})]})},b={component:()=>{let{user:r,enrollInMfa:o,ready:n}=u.usePrivyContext(),[t,m]=i.useState(null),{unenrollWithSms:j,unenrollWithTotp:x,unenrollWithPasskey:y,submitEnrollmentWithTotp:p,initEnrollmentWithPasskey:q,submitEnrollmentWithPasskey:S,initEnrollmentWithTotp:w}=d.useMfaEnrollment(),{data:b,onUserCloseViaDialogOrKeybindRef:I}=u.usePrivyModal(),F=a.useAppConfig(),{closePrivyModal:P}=c.usePrivyInternal(),{promptMfa:E}=d.useMfa(),[T,B]=i.useState(!1),[W,L]=i.useState(null),[R,A]=i.useState(null),H=()=>{P({shouldCallAuthOnSuccess:!0}),o(!1),setTimeout((()=>{m(null),L(null)}),500)},[N,U]=i.useState(!1),[O,D]=i.useState();I.current=H;let V=r?.mfaMethods.includes("sms"),Y=!!r?.phone,$=r?.mfaMethods.includes("totp"),z=r?.mfaMethods.includes("passkey"),Q=V||$||z,X=r?.linkedAccounts.filter((e=>"passkey"===e.type)).map((e=>e.credentialId))??[];function G(){m(null),L(null)}async function K(e=X){U(!0);try{return await q(),await S({credentialIds:e},{removeForLogin:b?.mfaEnrollmentFlow?.shouldUnlinkOnUnenrollMfa}),b?.mfaEnrollmentFlow?.onSuccess(),H()}catch(e){D(e)}finally{U(!1)}}if(i.useEffect((()=>{Q&&B(!0)}),[Q]),!n||!r||!F/*#__PURE__*/)return e.jsxs(e.Fragment,{children:[/*#__PURE__*/e.jsx(s.ModalHeader,{onClose:H},"header"),/*#__PURE__*/e.jsx(f.AppLogoContainer,{children:/*#__PURE__*/e.jsx(h.MfaShieldIcon,{})}),/*#__PURE__*/e.jsx(f.Container,{children:/*#__PURE__*/e.jsx(l.Loader,{})}),/*#__PURE__*/e.jsx(s.ModalFooter,{})]});if("sms"===t)/*#__PURE__*/return e.jsxs(e.Fragment,{children:[/*#__PURE__*/e.jsx(s.ModalHeader,{backFn:G,onClose:H},"header"),/*#__PURE__*/e.jsx(f.IconWrapper,{style:{marginBottom:"1.5rem"},children:/*#__PURE__*/e.jsx(v.default,{})}),/*#__PURE__*/e.jsx(f.Title,{children:"Remove SMS verification?"}),/*#__PURE__*/e.jsxs(f.SubTitle,{children:["MFA adds an extra layer of security to your ",F?.name," account. Make sure you have other methods to secure your account."]}),/*#__PURE__*/e.jsx(f.BottomSection,{children:/*#__PURE__*/e.jsx(s.PrimaryButton,{$warn:!0,onClick:async function(){m(null);try{await j()}catch(e){m(null)}},children:"Remove"})}),/*#__PURE__*/e.jsx(s.ModalFooter,{})]});if("totp"===t)/*#__PURE__*/return e.jsxs(e.Fragment,{children:[/*#__PURE__*/e.jsx(s.ModalHeader,{backFn:G,onClose:H},"header"),/*#__PURE__*/e.jsx(f.IconWrapper,{style:{marginBottom:"1.5rem"},children:/*#__PURE__*/e.jsx(v.default,{})}),/*#__PURE__*/e.jsx(f.Title,{children:"Remove authenticator app verification?"}),/*#__PURE__*/e.jsxs(f.SubTitle,{children:["MFA adds an extra layer of security to your ",F?.name," account. Make sure you have other methods to secure your account."]}),/*#__PURE__*/e.jsx(f.BottomSection,{children:/*#__PURE__*/e.jsx(s.PrimaryButton,{$warn:!0,onClick:async function(){m(null);try{await x()}catch(e){m(null)}},children:"Remove"})}),/*#__PURE__*/e.jsx(s.ModalFooter,{})]});if("passkey"===t){let r=b?.mfaEnrollmentFlow?.shouldUnlinkOnUnenrollMfa??!0;/*#__PURE__*/return e.jsxs(e.Fragment,{children:[/*#__PURE__*/e.jsx(s.ModalHeader,{backFn:G,onClose:H},"header"),/*#__PURE__*/e.jsx(f.IconWrapper,{style:{marginBottom:"1.5rem"},children:/*#__PURE__*/e.jsx(v.default,{})}),/*#__PURE__*/e.jsx(f.Title,{children:"Are you sure you want to remove this passkey?"}),/*#__PURE__*/e.jsx(f.SubTitle,{children:r?"Removing your passkey will remove as both a verification method and a login method.":"Removing your passkey will remove as a verification method."}),/*#__PURE__*/e.jsx(f.BottomSection,{children:/*#__PURE__*/e.jsx(s.PrimaryButton,{$warn:!0,onClick:async function(){m(null);try{await y({removeForLogin:b?.mfaEnrollmentFlow?.shouldUnlinkOnUnenrollMfa})}catch(e){m(null)}},children:"Remove"})}),/*#__PURE__*/e.jsx(s.ModalFooter,{})]})}if(0===b.mfaEnrollmentFlow.mfaMethods.length&&!Q)/*#__PURE__*/return e.jsxs(e.Fragment,{children:[/*#__PURE__*/e.jsx(s.ModalHeader,{onClose:H},"header"),/*#__PURE__*/e.jsx(f.IconWrapper,{style:{marginBottom:"1.5rem"},children:/*#__PURE__*/e.jsx(C.default,{})}),/*#__PURE__*/e.jsx(f.Title,{children:"Add more security"}),/*#__PURE__*/e.jsxs(f.SubTitle,{children:[F?.name," does not have any verification methods enabled."]}),/*#__PURE__*/e.jsx(f.BottomSection,{children:/*#__PURE__*/e.jsx(s.PrimaryButton,{onClick:H,children:"Close"})}),/*#__PURE__*/e.jsx(s.ModalFooter,{})]});let Z=!Q&&!T;return Z?/*#__PURE__*/e.jsxs(e.Fragment,{children:[/*#__PURE__*/e.jsx(s.ModalHeader,{onClose:H},"header"),/*#__PURE__*/e.jsx(f.IconWrapper,{style:{marginBottom:"1.5rem"},children:/*#__PURE__*/e.jsx(C.default,{})}),/*#__PURE__*/e.jsx(f.Title,{children:"Transaction Protection"}),/*#__PURE__*/e.jsx(f.SubTitle,{children:"Set up transaction protection to add an extra layer of security to your account"}),/*#__PURE__*/e.jsxs(f.List,{children:[/*#__PURE__*/e.jsxs(f.ListItem,{children:[/*#__PURE__*/e.jsx(f.ListItemIcon,{children:/*#__PURE__*/e.jsx(M.default,{})}),"Enable 2-Step verification for your ",F?.name," wallet."]}),/*#__PURE__*/e.jsxs(f.ListItem,{children:[/*#__PURE__*/e.jsx(f.ListItemIcon,{children:/*#__PURE__*/e.jsx(g.default,{})}),"You'll be prompted to authenticate to complete transactions."]})]}),/*#__PURE__*/e.jsxs(f.BottomSection,{children:[/*#__PURE__*/e.jsx(s.PrimaryButton,{onClick:()=>B(!0),children:"Continue"}),/*#__PURE__*/e.jsx(s.SecondaryButton,{onClick:H,children:"Not now"})]}),/*#__PURE__*/e.jsx(s.ModalFooter,{})]}):"sms"===W?/*#__PURE__*/e.jsx(k,{appName:F?.name||"Privy",onComplete:H,onReset:G,onClose:H}):"totp"===W&&R?/*#__PURE__*/e.jsx(h.EnrollTotp,{onClose:H,onReset:G,submitEnrollmentWithTotp:({mfaCode:e})=>async function(e){try{return D(void 0),await p({mfaCode:e}),b?.mfaEnrollmentFlow?.onSuccess(),H()}catch(e){D(e)}finally{m(null)}}(e),totpInfo:{...R,appName:F?.name||"Privy"}}):"passkey"===W?/*#__PURE__*/e.jsx(h.EnrollPasskey,{onReset:G,onClose:H,submitEnrollmentWithPasskey:K}):/*#__PURE__*/e.jsx(h.EnrollLandingScreen,{showIntro:Z,userMfaMethods:r.mfaMethods,appMfaMethods:F.mfa.methods,userHasAuthSms:Y,backFn:function(){B(!1)},handleSelectMethod:async function(e){try{await E()}catch(e){return void D(e)}return"totp"===e?(L(e),A(null),void w().then((e=>{A(e)})).catch((()=>{A(null),G()}))):"passkey"===e&&1===X.length?await K():void L(e)},isTotpLoading:"totp"===W&&!R,isPasskeyLoading:N,error:O,onClose:H,setRemovingMfaMethod:async e=>{try{await E()}catch(e){return void D(e)}m(e)}})}};exports.MfaEnrollmentFlowScreen=b,exports.default=b;