@privy-io/react-auth
Version:
React client for the Privy Auth API
2 lines (1 loc) • 9.31 kB
JavaScript
import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import{useState as o,useEffect as i,useRef as a}from"react";import{isMobile as n,isIOS as s}from"react-device-detect";import{styled as l}from"styled-components";import{L as c}from"./useActiveWallet-Bsy5GTCV.mjs";import{O as d}from"./OpenLink-BpeNYBKs.mjs";import{Q as m}from"./QrCode-DR5LogOp.mjs";import{Check as p,Copy as u}from"lucide-react";import{S as h}from"./Button-B8mqatRx.mjs";import{L as f}from"./LabelXs-BOisBtqT.mjs";import{u as g,q as v}from"./context-Bycd1SKV.mjs";import{u as y,b as C,k as w}from"./internal-context-e-Eni5bG.mjs";import{u as b,a as x}from"./get-is-unified-wallet-DvKS5IUO.mjs";import{s as T}from"./shouldProceedtoEmbeddedWalletCreationFlow-Djgu4xiS.mjs";import{S as k}from"./ScreenLayout-CddsD3Of.mjs";import{F as S}from"./farcaster-DPlSjvF5.mjs";import"zustand";import"./prepareFundingModalData-CIgFwA7s.mjs";import"@privy-io/js-sdk-core";import"eventemitter3";import"./events-context-CI0iqAXA.mjs";import"viem";import"viem/utils";import"./getPublicClient-A9RSftUZ.mjs";import"./useWallets-D0lpBS44.mjs";import"qrcode";import"tinycolor2";import"ofetch";import"./index-CCyaupaz.mjs";import"uuid";import"jose";import"@coinbase/wallet-sdk";import"@privy-io/ethereum";import"mipd";import"@privy-io/popup";import"./paths-3HW55qZg.mjs";import"./usePrivy-BaiIHGte.mjs";import"@scure/base";import"@headlessui/react";import"@walletconnect/ethereum-provider";import"@privy-io/urls";import"js-cookie";import"./frame-CwE9r3cT.mjs";import"@privy-io/routes";import"x402/client";import"@privy-io/api-base";import"viem/accounts";import"./use-sign-with-user-signer-eEm9Olt_.mjs";import"./getEmbeddedConnectedWallet-CM6cDQCS.mjs";import"./Screen-BTyXgDrd.mjs";import"./ModalHeader-BLGC-7ev.mjs";import"@heroicons/react/24/outline/ArrowLeftIcon";import"@heroicons/react/24/outline/ArrowRightIcon";import"@heroicons/react/24/outline/QuestionMarkCircleIcon";import"@heroicons/react/24/outline/XMarkIcon";import"./index-CJMgUOnw.mjs";let E=/*#__PURE__*/l.div.withConfig({displayName:"Container",componentId:"sc-913deead-0"})(["width:100%;"]),I=/*#__PURE__*/l.div.withConfig({displayName:"InputContainer",componentId:"sc-913deead-1"})(["display:flex;align-items:center;justify-content:space-between;gap:0.75rem;padding:0.75rem;height:56px;background:",";border:1px solid var(--privy-color-foreground-4);border-radius:var(--privy-border-radius-md);&:hover{border-color:",";}"],(e=>e.$disabled?"var(--privy-color-background-2)":"var(--privy-color-background)"),(e=>e.$disabled?"var(--privy-color-foreground-4)":"var(--privy-color-foreground-3)")),j=/*#__PURE__*/l.div.withConfig({displayName:"TextContainer",componentId:"sc-913deead-2"})(["flex:1;min-width:0;display:flex;align-items:center;"]),N=/*#__PURE__*/l.span.withConfig({displayName:"Text",componentId:"sc-913deead-3"})(["display:block;font-size:16px;line-height:24px;color:",";overflow:hidden;text-overflow:ellipsis;display:-webkit-box;-webkit-line-clamp:1;-webkit-box-orient:vertical;word-break:break-all;@media (min-width:441px){font-size:14px;line-height:20px;}"],(e=>e.$disabled?"var(--privy-color-foreground-2)":"var(--privy-color-foreground)")),A=/*#__PURE__*/l(N).withConfig({displayName:"PlaceholderText",componentId:"sc-913deead-4"})(["color:var(--privy-color-foreground-3);font-style:italic;"]),L=/*#__PURE__*/l(f).withConfig({displayName:"TitleLabel",componentId:"sc-913deead-5"})(["margin-bottom:0.5rem;"]),F=/*#__PURE__*/l(h).withConfig({displayName:"CopyButton",componentId:"sc-913deead-6"})(["&&{gap:0.375rem;font-size:14px;flex-shrink:0;}"]);const O=({value:a,title:n,placeholder:s,className:l,showCopyButton:c=!0,truncate:d,maxLength:m=40,disabled:h=!1})=>{let[f,g]=o(!1),v=d&&a?((e,t,r)=>{if((e=e.startsWith("https://")?e.slice(8):e).length<=r)return e;if("middle"===t){let t=Math.ceil(r/2)-2,o=Math.floor(r/2)-1;return`${e.slice(0,t)}...${e.slice(-o)}`}return`${e.slice(0,r-3)}...`})(a,d,m):a;return i((()=>{if(f){let e=setTimeout((()=>g(!1)),3e3);return()=>clearTimeout(e)}}),[f]),/*#__PURE__*/e(E,{className:l,children:[n&&/*#__PURE__*/t(L,{children:n}),/*#__PURE__*/e(I,{$disabled:h,children:[/*#__PURE__*/t(j,{children:a?/*#__PURE__*/t(N,{$disabled:h,title:a,children:v}):/*#__PURE__*/t(A,{$disabled:h,children:s||"No value"})}),c&&a&&/*#__PURE__*/t(F,{onClick:function(e){e.stopPropagation(),navigator.clipboard.writeText(a).then((()=>g(!0))).catch(console.error)},size:"sm",children:/*#__PURE__*/e(r,f?{children:["Copied",/*#__PURE__*/t(p,{size:14})]}:{children:["Copy",/*#__PURE__*/t(u,{size:14})]})})]})]})},_=({connectUri:r,loading:o,success:i,errorMessage:a,onBack:l,onClose:p,onOpenFarcaster:u})=>/*#__PURE__*/t(k,n||o?s?{title:a?a.message:"Sign in with Farcaster",subtitle:a?a.detail:"To sign in with Farcaster, please open the Farcaster app.",icon:S,iconVariant:"loading",iconLoadingStatus:{success:i,fail:!!a},primaryCta:r&&u?{label:"Open Farcaster app",onClick:u}:void 0,onBack:l,onClose:p,watermark:!0}:{title:a?a.message:"Signing in with Farcaster",subtitle:a?a.detail:"This should only take a moment",icon:S,iconVariant:"loading",iconLoadingStatus:{success:i,fail:!!a},onBack:l,onClose:p,watermark:!0,children:r&&n&&/*#__PURE__*/t(M,{children:/*#__PURE__*/t(d,{text:"Take me to Farcaster",url:r,color:"#8a63d2"})})}:{title:"Sign in with Farcaster",subtitle:"Scan with your phone's camera to continue.",onBack:l,onClose:p,watermark:!0,children:/*#__PURE__*/e(U,{children:[/*#__PURE__*/t(W,{children:r?/*#__PURE__*/t(m,{url:r,size:275,squareLogoElement:S}):/*#__PURE__*/t(z,{children:/*#__PURE__*/t(c,{})})}),/*#__PURE__*/e(D,{children:[/*#__PURE__*/t(B,{children:"Or copy this link and paste it into a phone browser to open the Farcaster app."}),r&&/*#__PURE__*/t(O,{value:r,truncate:"end",maxLength:30,showCopyButton:!0,disabled:!0})]})]})}),R={component:()=>{let{authenticated:e,logout:r,ready:n,user:s}=b(),{lastScreen:l,navigate:c,navigateBack:d,setModalData:m}=x(),p=g(),{getAuthFlow:u,loginWithFarcaster:h,closePrivyModal:f,createAnalyticsEvent:k}=y(),[S,E]=o(void 0),[I,j]=o(!1),[N,A]=o(!1),L=a([]),F=u(),O=F?.meta.connectUri;return i((()=>{let e=Date.now(),t=setInterval((async()=>{let r=await F.pollForReady.execute(),o=Date.now()-e;if(r){clearInterval(t),j(!0);try{await h(),A(!0)}catch(e){let t={retryable:!1,message:"Authentication failed"};if(e?.privyErrorCode===C.ALLOWLIST_REJECTED)return void c("AllowlistRejectionScreen");if(e?.privyErrorCode===C.USER_LIMIT_REACHED)return console.error(new w(e).toString()),void c("UserLimitReachedScreen");if(e?.privyErrorCode===C.USER_DOES_NOT_EXIST)return void c("AccountNotFoundScreen");if(e?.privyErrorCode===C.LINKED_TO_ANOTHER_USER)t.detail=e.message??"This account has already been linked to another user.";else{if(e?.privyErrorCode===C.ACCOUNT_TRANSFER_REQUIRED&&e.data?.data?.nonce)return m({accountTransfer:{nonce:e.data?.data?.nonce,account:e.data?.data?.subject,displayName:e.data?.data?.account?.displayName,linkMethod:"farcaster",embeddedWalletAddress:e.data?.data?.otherUser?.embeddedWalletAddress,farcasterEmbeddedAddress:e.data?.data?.otherUser?.farcasterEmbeddedAddress}}),void c("LinkConflictScreen");e?.privyErrorCode===C.INVALID_CREDENTIALS?(t.retryable=!0,t.detail="Something went wrong. Try again."):e?.privyErrorCode===C.TOO_MANY_REQUESTS&&(t.detail="Too many requests. Please wait before trying again.")}E(t)}}else o>12e4&&(clearInterval(t),E({retryable:!0,message:"Authentication failed",detail:"The request timed out. Try again."}))}),2e3);return()=>{clearInterval(t),L.current.forEach((e=>clearTimeout(e)))}}),[]),i((()=>{if(n&&e&&N&&s){if(p?.legal.requireUsersAcceptTerms&&!s.hasAcceptedTerms){let e=setTimeout((()=>{c("AffirmativeConsentScreen")}),v);return()=>clearTimeout(e)}N&&(T(s,p.embeddedWallets)?L.current.push(setTimeout((()=>{m({createWallet:{onSuccess:()=>{},onFailure:e=>{console.error(e),k({eventName:"embedded_wallet_creation_failure_logout",payload:{error:e,screen:"FarcasterConnectStatusScreen"}}),r()},callAuthOnSuccessOnClose:!0}}),c("EmbeddedWalletOnAccountCreateScreen")}),v)):L.current.push(setTimeout((()=>f({shouldCallAuthOnSuccess:!0,isSuccess:!0})),v)))}}),[N,n,e,s]),/*#__PURE__*/t(_,{connectUri:O,loading:I,success:N,errorMessage:S,onBack:l?d:void 0,onClose:f,onOpenFarcaster:()=>{O&&(window.location.href=O)}})}};let M=/*#__PURE__*/l.div.withConfig({displayName:"MobileLinkContainer",componentId:"sc-914339e1-0"})(["margin-top:24px;"]),U=/*#__PURE__*/l.div.withConfig({displayName:"ContentContainer",componentId:"sc-914339e1-1"})(["display:flex;flex-direction:column;align-items:center;gap:24px;"]),W=/*#__PURE__*/l.div.withConfig({displayName:"QrContainer",componentId:"sc-914339e1-2"})(["display:flex;align-items:center;justify-content:center;min-height:275px;"]),D=/*#__PURE__*/l.div.withConfig({displayName:"InstructionsContainer",componentId:"sc-914339e1-3"})(["display:flex;flex-direction:column;align-items:center;gap:16px;"]),B=/*#__PURE__*/l.div.withConfig({displayName:"InstructionText",componentId:"sc-914339e1-4"})(["font-size:0.875rem;text-align:center;color:var(--privy-color-foreground-2);"]),z=/*#__PURE__*/l.div.withConfig({displayName:"LoaderWrapper",componentId:"sc-914339e1-5"})(["position:relative;width:82px;height:82px;"]);export{R as FarcasterConnectStatusScreen,_ as FarcasterConnectStatusView,R as default};