UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

2 lines (1 loc) • 12.2 kB
"use strict";var e=require("@privy-io/are-addresses-equal"),t=require("react/jsx-runtime"),i=require("react"),a=require("./context-Cg3IEi80.js"),r=require("./internal-context-BJv4f_bO.js");function n(e){return"string"==typeof e&&/^custom:[a-zA-Z0-9_-]+$/i.test(e)}const l=e=>!!e.id&&"privy-v2"===e.recoveryMethod;function s(e){return e?new Date(1e3*e):null}function m(e,t){return e.slice().sort(((e,t)=>(e.firstVerifiedAt?.getTime()??0)-(t.firstVerifiedAt?.getTime()??0))).find((e=>e.type===t))}const d=e=>e?o(e).find((e=>0===e.walletIndex))??null:null,o=e=>e?p(e,"ethereum"):[],p=(e,t)=>e.linkedAccounts.filter((e=>"wallet"===e.type&&"privy"===e.walletClientType&&!e.imported&&e.chainType===t)),u=e=>e?p(e,"solana"):[],c=e=>e?u(e).find((e=>0===e.walletIndex))??null:null;function f({src:e,...i}){/*#__PURE__*/return t.jsx("img",{src:e,...i,style:{display:"none"}})}const _=/*#__PURE__*/i.createContext({currentScreen:null,lastScreen:null,navigate:r.notImplemented,navigateBack:r.notImplemented,resetNavigation:r.notImplemented,setModalData:r.notImplemented,onUserCloseViaDialogOrKeybindRef:void 0}),y=/*#__PURE__*/i.createContext({ready:!1,authenticated:!1,user:null,walletConnectors:null,connectWallet:r.notImplemented,login:r.notImplemented,connectOrCreateWallet:r.notImplemented,linkEmail:r.notImplemented,linkPhone:r.notImplemented,linkFarcaster:r.notImplemented,linkWallet:r.notImplemented,startCrossAppAuthFlow:r.notImplemented,linkGoogle:r.notImplemented,linkTwitter:r.notImplemented,linkTwitch:r.notImplemented,linkDiscord:r.notImplemented,linkGithub:r.notImplemented,linkSpotify:r.notImplemented,linkInstagram:r.notImplemented,linkTelegram:r.notImplemented,linkTiktok:r.notImplemented,linkLine:r.notImplemented,linkLinkedIn:r.notImplemented,linkApple:r.notImplemented,linkPasskey:r.notImplemented,linkOAuth:r.notImplemented,updateEmail:r.notImplemented,updatePhone:r.notImplemented,logout:r.notImplemented,getAccessToken:r.notImplemented,unlinkEmail:r.notImplemented,unlinkPhone:r.notImplemented,unlinkWallet:r.notImplemented,unlinkGoogle:r.notImplemented,unlinkTwitter:r.notImplemented,unlinkTwitch:r.notImplemented,unlinkDiscord:r.notImplemented,unlinkGithub:r.notImplemented,unlinkSpotify:r.notImplemented,unlinkInstagram:r.notImplemented,unlinkTiktok:r.notImplemented,unlinkLine:r.notImplemented,unlinkLinkedIn:r.notImplemented,unlinkApple:r.notImplemented,unlinkCrossAppAccount:r.notImplemented,unlinkFarcaster:r.notImplemented,unlinkTelegram:r.notImplemented,unlinkPasskey:r.notImplemented,unlinkOAuth:r.notImplemented,signMessage:r.notImplemented,signTypedData:r.notImplemented,enrollInMfa:r.notImplemented,initEnrollmentWithSms:r.notImplemented,initEnrollmentWithTotp:r.notImplemented,initEnrollmentWithPasskey:r.notImplemented,promptMfa:r.notImplemented,init:r.notImplemented,submitEnrollmentWithSms:r.notImplemented,submitEnrollmentWithTotp:r.notImplemented,submitEnrollmentWithPasskey:r.notImplemented,unenroll:r.notImplemented,submit:r.notImplemented,cancel:r.notImplemented,sendTransaction:r.notImplemented,signTransaction:r.notImplemented,setWalletPassword:r.notImplemented,setWalletRecovery:r.notImplemented,requestFarcasterSignerFromWarpcast:r.notImplemented,getFarcasterSignerPublicKey:r.notImplemented,signFarcasterMessage:r.notImplemented,signMessageWithCrossAppWallet:r.notImplemented,signTypedDataWithCrossAppWallet:r.notImplemented,sendTransactionWithCrossAppWallet:r.notImplemented,signTransactionWithCrossAppWallet:r.notImplemented,isModalOpen:!1,mfaMethods:[]});exports.ModalProvider=e=>{let r=a.useAppConfig(),[n,l]=i.useState(e.initialScreen),s=i.useRef(null);i.useEffect((()=>{e.open||(s.current=null)}),[e.open]),i.useEffect((()=>{s.current=null}),[e.initialScreen]);let m={data:e.data,setModalData:e.setModalData,currentScreen:e.initialScreen,lastScreen:n,navigate:(t,i=!0)=>{e.setInitialScreen(t),i&&l(e.initialScreen)},navigateBack:()=>{e.setInitialScreen(n)},resetNavigation:()=>{e.setInitialScreen(null),l(null)},onUserCloseViaDialogOrKeybindRef:s};/*#__PURE__*/return t.jsxs(_.Provider,{value:m,children:[("string"==typeof r.appearance.logo||"img"===r.appearance.logo?.type)&&/*#__PURE__*/t.jsx(f,{src:"string"==typeof r.appearance.logo?r.appearance.logo:r.appearance.logo.props.src}),e.children]})},exports.PrivyContext=y,exports.convertUserResponseToUser=function(e){if(!e)return null;let t=function(e){let t=[];for(let i of e){let e=i.type;switch(i.type){case"wallet":let a={id:i.id,address:i.address,type:i.type,imported:i.imported,delegated:i.delegated,firstVerifiedAt:s(i.first_verified_at),latestVerifiedAt:s(i.latest_verified_at),chainType:i.chain_type,walletClientType:i.wallet_client_type,connectorType:i.connector_type,recoveryMethod:i.recovery_method,walletIndex:i.wallet_index,publicKey:i.public_key};t.push(a);break;case"smart_wallet":let r={address:i.address,type:i.type,firstVerifiedAt:s(i.first_verified_at),latestVerifiedAt:s(i.latest_verified_at),smartWalletType:i.smart_wallet_type,smartWalletVersion:i.smart_wallet_version};t.push(r);break;case"cross_app":let l={type:i.type,subject:i.subject,embeddedWallets:i.embedded_wallets,smartWallets:i.smart_wallets,providerApp:{id:i.provider_app_id},firstVerifiedAt:s(i.first_verified_at),latestVerifiedAt:s(i.latest_verified_at)};t.push(l);break;case"email":let m={address:i.address,type:i.type,firstVerifiedAt:s(i.first_verified_at),latestVerifiedAt:s(i.latest_verified_at)};t.push(m);break;case"phone":let d={number:i.phoneNumber,type:i.type,firstVerifiedAt:s(i.first_verified_at),latestVerifiedAt:s(i.latest_verified_at)};t.push(d);break;case"google_oauth":let o={subject:i.subject,email:i.email,name:i.name,type:i.type,firstVerifiedAt:s(i.first_verified_at),latestVerifiedAt:s(i.latest_verified_at)};t.push(o);break;case"spotify_oauth":let p={subject:i.subject,email:i.email,name:i.name,type:i.type,firstVerifiedAt:s(i.first_verified_at),latestVerifiedAt:s(i.latest_verified_at)};t.push(p);break;case"instagram_oauth":let u={subject:i.subject,username:i.username,type:i.type,firstVerifiedAt:s(i.first_verified_at),latestVerifiedAt:s(i.latest_verified_at)};t.push(u);break;case"twitter_oauth":let c={subject:i.subject,username:i.username,name:i.name,type:i.type,profilePictureUrl:i.profile_picture_url,firstVerifiedAt:s(i.first_verified_at),latestVerifiedAt:s(i.latest_verified_at)};t.push(c);break;case"discord_oauth":let f={subject:i.subject,username:i.username,email:i.email,type:i.type,firstVerifiedAt:s(i.first_verified_at),latestVerifiedAt:s(i.latest_verified_at)};t.push(f);break;case"github_oauth":let _={subject:i.subject,username:i.username,name:i.name,email:i.email,type:i.type,firstVerifiedAt:s(i.first_verified_at),latestVerifiedAt:s(i.latest_verified_at)};t.push(_);break;case"tiktok_oauth":let y={subject:i.subject,username:i.username,name:i.name,type:i.type,firstVerifiedAt:s(i.first_verified_at),latestVerifiedAt:s(i.latest_verified_at)};t.push(y);break;case"line_oauth":let h={subject:i.subject,name:i.name,email:i.email,profilePictureUrl:i.profile_picture_url,type:i.type,firstVerifiedAt:s(i.first_verified_at),latestVerifiedAt:s(i.latest_verified_at)};t.push(h);break;case"twitch_oauth":let b={subject:i.subject,username:i.username,type:i.type,firstVerifiedAt:s(i.first_verified_at),latestVerifiedAt:s(i.latest_verified_at)};t.push(b);break;case"linkedin_oauth":let I={subject:i.subject,name:i.name,email:i.email,vanityName:i.vanity_name,type:i.type,firstVerifiedAt:s(i.first_verified_at),latestVerifiedAt:s(i.latest_verified_at)};t.push(I);break;case"apple_oauth":let v={subject:i.subject,email:i.email,type:i.type,firstVerifiedAt:s(i.first_verified_at),latestVerifiedAt:s(i.latest_verified_at)};t.push(v);break;case"custom_auth":t.push({type:i.type,customUserId:i.custom_user_id,firstVerifiedAt:s(i.first_verified_at),latestVerifiedAt:s(i.latest_verified_at)});break;case"farcaster":let k={type:i.type,fid:i.fid,ownerAddress:i.owner_address,displayName:i.display_name,username:i.username,bio:i.bio,pfp:i.profile_picture_url,url:i.homepage_url,firstVerifiedAt:s(i.first_verified_at),latestVerifiedAt:s(i.latest_verified_at),signerPublicKey:i.signer_public_key};t.push(k);break;case"passkey":let g={type:i.type,enrolledInMfa:i.enrolled_in_mfa,credentialId:i.credential_id,publicKey:i.public_key,authenticatorName:i.authenticator_name,createdWithDevice:i.created_with_device,createdWithOs:i.created_with_os,createdWithBrowser:i.created_with_browser,firstVerifiedAt:s(i.first_verified_at),latestVerifiedAt:s(i.latest_verified_at)};t.push(g);break;case"telegram":let A={type:i.type,telegramUserId:i.telegram_user_id,firstName:i.first_name,lastName:i.last_name,username:i.username,photoUrl:i.photo_url,firstVerifiedAt:s(i.first_verified_at),latestVerifiedAt:s(i.latest_verified_at)};t.push(A);break;default:if(n(i.type)){let e={type:i.type,subject:i.subject,name:i.name,username:i.username,email:i.email,profilePictureUrl:i.profile_picture_url,firstVerifiedAt:s(i.first_verified_at),latestVerifiedAt:s(i.latest_verified_at)};t.push(e);break}console.warn(`Unrecognized account type: ${e}. Please consider upgrading the Privy SDK.`)}}return t}(e.linked_accounts),i=m(t,"wallet"),a=m(t,"smart_wallet"),r=m(t,"email"),l=m(t,"phone"),d=m(t,"google_oauth"),o=m(t,"twitter_oauth"),p=m(t,"discord_oauth"),u=m(t,"github_oauth"),c=m(t,"spotify_oauth"),f=m(t,"instagram_oauth"),_=m(t,"tiktok_oauth"),y=m(t,"line_oauth"),h=m(t,"twitch_oauth"),b=m(t,"linkedin_oauth"),I=m(t,"apple_oauth"),v=m(t,"farcaster"),k=m(t,"telegram"),g=e.mfa_methods.map((({type:e,verified_at:t})=>({type:e,verifiedAt:s(t)})));return{id:e.id,createdAt:s(e.created_at),linkedAccounts:t,email:r&&{address:r?.address},phone:l&&{number:l?.number},wallet:i&&{id:i.id,address:i.address,chainType:i.chainType,walletClientType:i.walletClientType,connectorType:i.connectorType,recoveryMethod:i.recoveryMethod,imported:i.imported,delegated:i.delegated,walletIndex:i.walletIndex,publicKey:i.publicKey},smartWallet:a&&{address:a.address,smartWalletType:a.smartWalletType,smartWalletVersion:a.smartWalletVersion},google:d&&{subject:d.subject,email:d.email,name:d.name},twitter:o&&{subject:o.subject,username:o.username,name:o.name,profilePictureUrl:o.profilePictureUrl},discord:p&&{subject:p.subject,username:p.username,email:p.email},github:u&&{subject:u.subject,username:u.username,name:u.name,email:u.email},spotify:c&&{subject:c.subject,email:c.email,name:c.name},instagram:f&&{subject:f.subject,username:f.username},tiktok:_&&{subject:_.subject,username:_.username,name:_.name},line:y&&{subject:y.subject,name:y.name,email:y.email,profilePictureUrl:y.profilePictureUrl},twitch:h&&{subject:h.subject,username:h.username},linkedin:b&&{subject:b.subject,name:b.name,email:b.email,vanityName:b.vanityName},apple:I&&{subject:I.subject,email:I.email},farcaster:v&&{fid:v.fid,ownerAddress:v.ownerAddress,displayName:v.displayName,username:v.username,bio:v.bio,pfp:v.pfp,url:v.url,signerPublicKey:v.signerPublicKey},telegram:k&&{telegramUserId:k.telegramUserId,firstName:k.firstName,lastName:k.lastName,username:k.username,photoUrl:k.photoUrl},delegatedWallets:[],mfaMethods:g.map((e=>e.type)),hasAcceptedTerms:e.has_accepted_terms??!1,isGuest:e.is_guest,customMetadata:e.custom_metadata}},exports.getAllPrivyWalletsForChainType=p,exports.getConnectedWalletWithAddress=(t,i)=>t.find((t=>e.areAddressesEqual(t.address,i)))||null,exports.getImportedPrivyEthereumWallets=e=>(e?.linkedAccounts??[]).filter((e=>"wallet"===e.type&&"privy"===e.walletClientType&&e.imported&&"ethereum"===e.chainType)),exports.getImportedPrivySolanaWallets=e=>(e?.linkedAccounts??[]).filter((e=>"wallet"===e.type&&e.imported&&"privy"===e.walletClientType&&"solana"===e.chainType)),exports.getIsUnifiedWallet=l,exports.getPrivyEthereumHDWallets=o,exports.getPrivyEthereumWallet=d,exports.getPrivyPrimaryWallet=e=>{let t=d(e),i=c(e);return t&&i&&!l(i)&&l(t)?i:d(e)??c(e)},exports.getPrivySolanaHDWallets=u,exports.getPrivySolanaWallet=c,exports.getPrivyWalletWithAddress=(t,i)=>t?.linkedAccounts.find((t=>"wallet"===t.type&&"privy"===t.walletClientType&&e.areAddressesEqual(t.address,i)))||null,exports.isCustomOAuthProvider=n,exports.usePrivyContext=()=>i.useContext(y),exports.usePrivyModal=()=>i.useContext(_);