UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

2 lines (1 loc) • 12.6 kB
"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("./Button-Bm_Ft0Dw.js"),l=require("./useActiveWallet-BOX8xCn9.js"),a=require("./ModalHeader-DB7Tbjwz.js"),c=require("./context-DFq1obBO.js"),u=require("./internal-context-NWsAL807.js"),d=require("./get-is-unified-wallet-D4ouXJpi.js"),h=require("./index-NJrQkxJ7.js"),m=require("./EnrollTotp-BVeDTQ5J.js"),j=require("@heroicons/react/24/outline/CheckCircleIcon"),x=require("@heroicons/react/24/outline/PhoneIcon"),y=require("@privy-io/js-sdk-core"),f=require("./ConnectPhoneForm-WT8rso3i.js"),p=require("./PinInput-DSI6rjOn.js"),q=require("./ErrorScreen-BSjRGrRR.js");function v(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}require("styled-components"),require("zustand"),require("react-device-detect"),require("./prepareFundingModalData-Cnd6rhhJ.js"),require("eventemitter3"),require("./events-context-Di6--rDg.js"),require("viem"),require("viem/utils"),require("./getPublicClient-CGlodIp_.js"),require("./useWallets-BLFNjTwC.js"),require("@heroicons/react/24/outline/ArrowLeftIcon"),require("@heroicons/react/24/outline/ArrowRightIcon"),require("@heroicons/react/24/outline/XMarkIcon"),require("tinycolor2"),require("ofetch"),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-CcfSRv1R.js"),require("@scure/base"),require("@headlessui/react"),require("@walletconnect/ethereum-provider"),require("@privy-io/urls"),require("js-cookie"),require("./frame-B14fp9oC.js"),require("@privy-io/routes"),require("x402/client"),require("@privy-io/api-base"),require("viem/accounts"),require("./use-sign-with-user-signer-BcAsQTGF.js"),require("./getEmbeddedConnectedWallet-6TsVZJkD.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-ChrHHazc.js"),require("./LoadingSkeleton-DoCrfGnl.js"),require("@heroicons/react/24/outline/ArrowRightEndOnRectangleIcon"),require("@heroicons/react/24/outline/ClockIcon"),require("./LinkPasskeyScreen-CUMuhJ2Q.js"),require("lucide-react"),require("./TodoList-Cm_a93ef.js"),require("./ScreenLayout-BeH-Q6N2.js"),require("./Screen-D9Tvi297.js"),require("./index-CRFXS9pP.js"),require("./CopyToClipboard-fd8Z3kQX.js"),require("./copy-BiZ7dtqy.js"),require("./Layouts-dRL0OSpp.js"),require("./Column-C2bAbJE0.js"),require("./LabelXs-CYMv6rOZ.js"),require("./Subtitle-Dtmrw8l5.js"),require("./Title-C-tHJvrb.js"),require("./shared-BPGwTh8c.js"),require("@heroicons/react/24/solid/ShieldCheckIcon"),require("./QrCode-DsQD8cG6.js"),require("qrcode"),require("./reservoir-CQGyEBb7.js");var C=/*#__PURE__*/v(r),M=/*#__PURE__*/v(o),S=/*#__PURE__*/v(n),g=/*#__PURE__*/v(t),w=/*#__PURE__*/v(j),k=/*#__PURE__*/v(x);const b=({appName:r,onComplete:o,onReset:n,onClose:t})=>{let[l,u]=i.useState(""),[m,j]=i.useState(!1),[x,v]=i.useState(null),[C,M]=i.useState("enroll"),{initEnrollmentWithSms:S,submitEnrollmentWithSms:g}=h.useMfaEnrollment(),{data:b}=d.usePrivyModal(),I=c.useAppConfig();function F(){b?.mfaEnrollmentFlow?.onSuccess(),o()}return x?/*#__PURE__*/e.jsx(q.ErrorScreenView,{allowlistConfig:I.allowlistConfig,error:x,onBack:()=>v(null),onRetry:()=>v(null)}):"enroll"===C?/*#__PURE__*/e.jsxs(e.Fragment,{children:[/*#__PURE__*/e.jsx(a.ModalHeader,{backFn:n,onClose:t},"header"),/*#__PURE__*/e.jsx(p.IconWrapper,{style:{marginBottom:"1.5rem"},children:/*#__PURE__*/e.jsx(k.default,{})}),/*#__PURE__*/e.jsx(p.Title,{children:"Set up SMS verification"}),/*#__PURE__*/e.jsxs(p.SubTitle,{children:["We'll text a verification code to this mobile device whenever you use your ",r," ","wallet."]}),/*#__PURE__*/e.jsxs(p.Container,{children:[/*#__PURE__*/e.jsx(f.ConnectPhoneNumberForm,{onSubmit:async function({qualifiedPhoneNumber:e}){try{await S({phoneNumber:e}),u(e),M("verify")}catch(e){v(e)}},hideRecent:!0}),/*#__PURE__*/e.jsxs(p.TermsText,{children:["By providing your mobile number, you agree to receive text messages from ",I?.name,". Some carrier charges may apply"]})]}),/*#__PURE__*/e.jsx(a.ModalFooter,{})]}):m?/*#__PURE__*/e.jsxs(e.Fragment,{children:[/*#__PURE__*/e.jsx(a.ModalHeader,{onClose:F},"header"),/*#__PURE__*/e.jsx(p.IconWrapper,{style:{marginBottom:"1.5rem"},children:/*#__PURE__*/e.jsx(w.default,{})}),/*#__PURE__*/e.jsx(p.Title,{children:"SMS verification added"}),/*#__PURE__*/e.jsxs(p.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(p.BottomSection,{children:/*#__PURE__*/e.jsx(s.PrimaryButton,{onClick:F,children:"Done"})}),/*#__PURE__*/e.jsx(a.ModalFooter,{})]}):/*#__PURE__*/e.jsxs(e.Fragment,{children:[/*#__PURE__*/e.jsx(a.ModalHeader,{backFn:function(){"verify"===C?M("enroll"):n()},onClose:t},"header"),/*#__PURE__*/e.jsx(p.IconWrapper,{style:{marginBottom:"1.5rem"},children:/*#__PURE__*/e.jsx(k.default,{})}),/*#__PURE__*/e.jsx(p.Title,{children:"Enter enrollment code"}),/*#__PURE__*/e.jsxs(p.Container,{children:[/*#__PURE__*/e.jsx(p.NumericPinInput,{onChange:async function(e){try{if(!e)return;await g({phoneNumber:l,mfaCode:e}),j(!0)}catch(e){if(h.errorIndicatesMfaRateLimit(e))throw Error("You have exceeded the maximum number of attempts. Please close this window and try again in 10 seconds.");if(h.errorIndicatesMfaVerificationFailed(e))throw Error("The code you entered is not valid");if(h.errorIndicatesMfaTimeout(e))throw Error("You have exceeded the time limit for code entry. Please try again in 30 seconds.");throw h.errorIndicatesMfaCanceled(e)?Error("Verification canceled"):Error("Unknown error")}}}),/*#__PURE__*/e.jsxs(p.SubTitle,{children:["To continue, enter the 6-digit code sent to ",/*#__PURE__*/e.jsx("strong",{children:y.lastFourDigits(l)})]})]}),/*#__PURE__*/e.jsx(a.ModalFooter,{})]})},I={component:()=>{let{user:r,enrollInMfa:o,ready:n}=d.usePrivyContext(),[t,j]=i.useState(null),{unenrollWithSms:x,unenrollWithTotp:y,unenrollWithPasskey:f,submitEnrollmentWithTotp:q,initEnrollmentWithPasskey:v,submitEnrollmentWithPasskey:w,initEnrollmentWithTotp:k}=h.useMfaEnrollment(),{data:I,onUserCloseViaDialogOrKeybindRef:F}=d.usePrivyModal(),E=c.useAppConfig(),{closePrivyModal:P}=u.usePrivyInternal(),{promptMfa:T}=h.useMfa(),[B,W]=i.useState(!1),[L,R]=i.useState(null),[A,H]=i.useState(null),N=()=>{P({shouldCallAuthOnSuccess:!0}),o(!1),setTimeout((()=>{j(null),R(null)}),500)},[U,D]=i.useState(!1),[O,V]=i.useState();F.current=N;let Y=r?.mfaMethods.includes("sms"),$=!!r?.phone,K=r?.mfaMethods.includes("totp"),Q=r?.mfaMethods.includes("passkey"),X=Y||K||Q,z=r?.linkedAccounts.filter((e=>"passkey"===e.type)).map((e=>e.credentialId))??[];function J(){j(null),R(null)}async function G(e=z){D(!0);try{return await v(),await w({credentialIds:e},{removeForLogin:I?.mfaEnrollmentFlow?.shouldUnlinkOnUnenrollMfa}),I?.mfaEnrollmentFlow?.onSuccess(),N()}catch(e){V(e)}finally{D(!1)}}if(i.useEffect((()=>{X&&W(!0)}),[X]),!n||!r||!E/*#__PURE__*/)return e.jsxs(e.Fragment,{children:[/*#__PURE__*/e.jsx(a.ModalHeader,{onClose:N},"header"),/*#__PURE__*/e.jsx(p.AppLogoContainer,{children:/*#__PURE__*/e.jsx(m.MfaShieldIcon,{})}),/*#__PURE__*/e.jsx(p.Container,{children:/*#__PURE__*/e.jsx(l.Loader,{})}),/*#__PURE__*/e.jsx(a.ModalFooter,{})]});if("sms"===t)/*#__PURE__*/return e.jsxs(e.Fragment,{children:[/*#__PURE__*/e.jsx(a.ModalHeader,{backFn:J,onClose:N},"header"),/*#__PURE__*/e.jsx(p.IconWrapper,{style:{marginBottom:"1.5rem"},children:/*#__PURE__*/e.jsx(C.default,{})}),/*#__PURE__*/e.jsx(p.Title,{children:"Remove SMS verification?"}),/*#__PURE__*/e.jsxs(p.SubTitle,{children:["MFA adds an extra layer of security to your ",E?.name," account. Make sure you have other methods to secure your account."]}),/*#__PURE__*/e.jsx(p.BottomSection,{children:/*#__PURE__*/e.jsx(s.PrimaryButton,{$warn:!0,onClick:async function(){j(null);try{await x()}catch(e){j(null)}},children:"Remove"})}),/*#__PURE__*/e.jsx(a.ModalFooter,{})]});if("totp"===t)/*#__PURE__*/return e.jsxs(e.Fragment,{children:[/*#__PURE__*/e.jsx(a.ModalHeader,{backFn:J,onClose:N},"header"),/*#__PURE__*/e.jsx(p.IconWrapper,{style:{marginBottom:"1.5rem"},children:/*#__PURE__*/e.jsx(C.default,{})}),/*#__PURE__*/e.jsx(p.Title,{children:"Remove authenticator app verification?"}),/*#__PURE__*/e.jsxs(p.SubTitle,{children:["MFA adds an extra layer of security to your ",E?.name," account. Make sure you have other methods to secure your account."]}),/*#__PURE__*/e.jsx(p.BottomSection,{children:/*#__PURE__*/e.jsx(s.PrimaryButton,{$warn:!0,onClick:async function(){j(null);try{await y()}catch(e){j(null)}},children:"Remove"})}),/*#__PURE__*/e.jsx(a.ModalFooter,{})]});if("passkey"===t){let r=I?.mfaEnrollmentFlow?.shouldUnlinkOnUnenrollMfa??!0;/*#__PURE__*/return e.jsxs(e.Fragment,{children:[/*#__PURE__*/e.jsx(a.ModalHeader,{backFn:J,onClose:N},"header"),/*#__PURE__*/e.jsx(p.IconWrapper,{style:{marginBottom:"1.5rem"},children:/*#__PURE__*/e.jsx(C.default,{})}),/*#__PURE__*/e.jsx(p.Title,{children:"Are you sure you want to remove this passkey?"}),/*#__PURE__*/e.jsx(p.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(p.BottomSection,{children:/*#__PURE__*/e.jsx(s.PrimaryButton,{$warn:!0,onClick:async function(){j(null);try{await f({removeForLogin:I?.mfaEnrollmentFlow?.shouldUnlinkOnUnenrollMfa})}catch(e){j(null)}},children:"Remove"})}),/*#__PURE__*/e.jsx(a.ModalFooter,{})]})}if(0===I.mfaEnrollmentFlow.mfaMethods.length&&!X)/*#__PURE__*/return e.jsxs(e.Fragment,{children:[/*#__PURE__*/e.jsx(a.ModalHeader,{onClose:N},"header"),/*#__PURE__*/e.jsx(p.IconWrapper,{style:{marginBottom:"1.5rem"},children:/*#__PURE__*/e.jsx(M.default,{})}),/*#__PURE__*/e.jsx(p.Title,{children:"Add more security"}),/*#__PURE__*/e.jsxs(p.SubTitle,{children:[E?.name," does not have any verification methods enabled."]}),/*#__PURE__*/e.jsx(p.BottomSection,{children:/*#__PURE__*/e.jsx(s.PrimaryButton,{onClick:N,children:"Close"})}),/*#__PURE__*/e.jsx(a.ModalFooter,{})]});let Z=!X&&!B;return Z?/*#__PURE__*/e.jsxs(e.Fragment,{children:[/*#__PURE__*/e.jsx(a.ModalHeader,{onClose:N},"header"),/*#__PURE__*/e.jsx(p.IconWrapper,{style:{marginBottom:"1.5rem"},children:/*#__PURE__*/e.jsx(M.default,{})}),/*#__PURE__*/e.jsx(p.Title,{children:"Transaction Protection"}),/*#__PURE__*/e.jsx(p.SubTitle,{children:"Set up transaction protection to add an extra layer of security to your account"}),/*#__PURE__*/e.jsxs(p.List,{children:[/*#__PURE__*/e.jsxs(p.ListItem,{children:[/*#__PURE__*/e.jsx(p.ListItemIcon,{children:/*#__PURE__*/e.jsx(S.default,{})}),"Enable 2-Step verification for your ",E?.name," wallet."]}),/*#__PURE__*/e.jsxs(p.ListItem,{children:[/*#__PURE__*/e.jsx(p.ListItemIcon,{children:/*#__PURE__*/e.jsx(g.default,{})}),"You'll be prompted to authenticate to complete transactions."]})]}),/*#__PURE__*/e.jsxs(p.BottomSection,{children:[/*#__PURE__*/e.jsx(s.PrimaryButton,{onClick:()=>W(!0),children:"Continue"}),/*#__PURE__*/e.jsx(s.SecondaryButton,{onClick:N,children:"Not now"})]}),/*#__PURE__*/e.jsx(a.ModalFooter,{})]}):"sms"===L?/*#__PURE__*/e.jsx(b,{appName:E?.name||"Privy",onComplete:N,onReset:J,onClose:N}):"totp"===L&&A?/*#__PURE__*/e.jsx(m.EnrollTotp,{onClose:N,onReset:J,submitEnrollmentWithTotp:({mfaCode:e})=>async function(e){try{return V(void 0),await q({mfaCode:e}),I?.mfaEnrollmentFlow?.onSuccess(),N()}catch(e){V(e)}finally{j(null)}}(e),totpInfo:{...A,appName:E?.name||"Privy"}}):"passkey"===L?/*#__PURE__*/e.jsx(m.EnrollPasskey,{onReset:J,onClose:N,submitEnrollmentWithPasskey:G}):/*#__PURE__*/e.jsx(m.EnrollLandingScreen,{showIntro:Z,userMfaMethods:r.mfaMethods,appMfaMethods:E.mfa.methods,userHasAuthSms:$,backFn:function(){W(!1)},handleSelectMethod:async function(e){try{await T()}catch(e){return void V(e)}return"totp"===e?(R(e),H(null),void k().then((e=>{H(e)})).catch((()=>{H(null),J()}))):"passkey"===e&&1===z.length?await G():void R(e)},isTotpLoading:"totp"===L&&!A,isPasskeyLoading:U,error:O,onClose:N,setRemovingMfaMethod:async e=>{try{await T()}catch(e){return void V(e)}j(e)}})}};exports.MfaEnrollmentFlowScreen=I,exports.default=I;