UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

2 lines (1 loc) • 12.1 kB
"use strict";var e=require("viem"),t=require("react/jsx-runtime"),i=require("react"),a=require("./context-DFq1obBO.js"),r=require("./internal-context-NWsAL807.js");function n(e){return"string"==typeof e&&/^custom:[a-zA-Z0-9_-]+$/i.test(e)}function l(e){return e?new Date(1e3*e):null}function s(e,t){return e.slice().sort(((e,t)=>(e.firstVerifiedAt?.getTime()??0)-(t.firstVerifiedAt?.getTime()??0))).find((e=>e.type===t))}const m=e=>e?d(e).find((e=>0===e.walletIndex))??null:null,d=e=>e?o(e,"ethereum"):[],o=(e,t)=>e.linkedAccounts.filter((e=>"wallet"===e.type&&"privy"===e.walletClientType&&!e.imported&&e.chainType===t)),p=e=>e?o(e,"solana"):[],u=e=>e?p(e).find((e=>0===e.walletIndex))??null:null,c=(t,i)=>t===i||!(!t.startsWith("0x")||!i.startsWith("0x"))&&e.getAddress(t)===e.getAddress(i);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,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,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,exportWallet: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.areAddressesEqual=c,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:l(i.first_verified_at),latestVerifiedAt:l(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:l(i.first_verified_at),latestVerifiedAt:l(i.latest_verified_at),smartWalletType:i.smart_wallet_type,smartWalletVersion:i.smart_wallet_version};t.push(r);break;case"cross_app":let s={type:i.type,subject:i.subject,embeddedWallets:i.embedded_wallets,smartWallets:i.smart_wallets,providerApp:{id:i.provider_app_id},firstVerifiedAt:l(i.first_verified_at),latestVerifiedAt:l(i.latest_verified_at)};t.push(s);break;case"email":let m={address:i.address,type:i.type,firstVerifiedAt:l(i.first_verified_at),latestVerifiedAt:l(i.latest_verified_at)};t.push(m);break;case"phone":let d={number:i.phoneNumber,type:i.type,firstVerifiedAt:l(i.first_verified_at),latestVerifiedAt:l(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:l(i.first_verified_at),latestVerifiedAt:l(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:l(i.first_verified_at),latestVerifiedAt:l(i.latest_verified_at)};t.push(p);break;case"instagram_oauth":let u={subject:i.subject,username:i.username,type:i.type,firstVerifiedAt:l(i.first_verified_at),latestVerifiedAt:l(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:l(i.first_verified_at),latestVerifiedAt:l(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:l(i.first_verified_at),latestVerifiedAt:l(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:l(i.first_verified_at),latestVerifiedAt:l(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:l(i.first_verified_at),latestVerifiedAt:l(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:l(i.first_verified_at),latestVerifiedAt:l(i.latest_verified_at)};t.push(h);break;case"twitch_oauth":let b={subject:i.subject,username:i.username,type:i.type,firstVerifiedAt:l(i.first_verified_at),latestVerifiedAt:l(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:l(i.first_verified_at),latestVerifiedAt:l(i.latest_verified_at)};t.push(I);break;case"apple_oauth":let v={subject:i.subject,email:i.email,type:i.type,firstVerifiedAt:l(i.first_verified_at),latestVerifiedAt:l(i.latest_verified_at)};t.push(v);break;case"custom_auth":t.push({type:i.type,customUserId:i.custom_user_id,firstVerifiedAt:l(i.first_verified_at),latestVerifiedAt:l(i.latest_verified_at)});break;case"farcaster":let g={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:l(i.first_verified_at),latestVerifiedAt:l(i.latest_verified_at),signerPublicKey:i.signer_public_key};t.push(g);break;case"passkey":let k={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:l(i.first_verified_at),latestVerifiedAt:l(i.latest_verified_at)};t.push(k);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:l(i.first_verified_at),latestVerifiedAt:l(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:l(i.first_verified_at),latestVerifiedAt:l(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=s(t,"wallet"),a=s(t,"smart_wallet"),r=s(t,"email"),m=s(t,"phone"),d=s(t,"google_oauth"),o=s(t,"twitter_oauth"),p=s(t,"discord_oauth"),u=s(t,"github_oauth"),c=s(t,"spotify_oauth"),f=s(t,"instagram_oauth"),_=s(t,"tiktok_oauth"),y=s(t,"line_oauth"),h=s(t,"twitch_oauth"),b=s(t,"linkedin_oauth"),I=s(t,"apple_oauth"),v=s(t,"farcaster"),g=s(t,"telegram"),k=e.mfa_methods.map((({type:e,verified_at:t})=>({type:e,verifiedAt:l(t)})));return{id:e.id,createdAt:l(e.created_at),linkedAccounts:t,email:r&&{address:r?.address},phone:m&&{number:m?.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:g&&{telegramUserId:g.telegramUserId,firstName:g.firstName,lastName:g.lastName,username:g.username,photoUrl:g.photoUrl},delegatedWallets:[],mfaMethods:k.map((e=>e.type)),hasAcceptedTerms:e.has_accepted_terms??!1,isGuest:e.is_guest,customMetadata:e.custom_metadata}},exports.getAllPrivyWalletsForChainType=o,exports.getConnectedWalletWithAddress=(e,t)=>e.find((e=>c(e.address,t)))||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=e=>!!e.id&&"privy-v2"===e.recoveryMethod,exports.getPrivyEthereumHDWallets=d,exports.getPrivyEthereumWallet=m,exports.getPrivyPrimaryWallet=e=>m(e)??u(e),exports.getPrivySolanaHDWallets=p,exports.getPrivySolanaWallet=u,exports.getPrivyWalletWithAddress=(e,t)=>e?.linkedAccounts.find((e=>"wallet"===e.type&&"privy"===e.walletClientType&&c(e.address,t)))||null,exports.isCustomOAuthProvider=n,exports.usePrivyContext=()=>i.useContext(y),exports.usePrivyModal=()=>i.useContext(_);