@swisstronik/sdi-react-sdk-privy
Version:
Swisstronik Digital Identity React SDK, Privy edition. Easier setup for both web3 and web2 flows. Verify once, use anywhere.
90 lines (87 loc) • 3.29 kB
JavaScript
import { usePrivy, useWallets, useLogin, PrivyProvider } from '@privy-io/react-auth';
export { usePrivy, useWallets } from '@privy-io/react-auth';
import { useEffect } from 'react';
import { useSDI, SwisstronikDigitalIdentity, swisstronikMainnetChain, SDIContextProvider } from '@swisstronik/sdi-react-sdk';
export { useSDI } from '@swisstronik/sdi-react-sdk';
import { jsx } from 'react/jsx-runtime';
// src/index.tsx
var SwisstronikDigitalIdentityPrivy = () => {
const { ready, authenticated } = usePrivy();
const { setReady: setSDIReady, setModalContentStep, setIsMainModalOpen, setUserAddress, setEip1193Provider } = useSDI();
const { wallets, ready: walletsReady } = useWallets();
useEffect(() => {
if (!walletsReady || wallets.length === 0) return;
if (wallets.length > 0) {
console.log("Privy wallets changed");
const wallet = wallets[0];
console.log("Setting user active address to ", wallet.address);
setUserAddress(wallet.address);
wallet.getEthereumProvider().then((provider) => {
console.log("Setting provider to ", provider);
setEip1193Provider(provider);
});
}
}, [wallets, walletsReady]);
useEffect(() => {
setSDIReady(true);
}, []);
const handleOpenModal = () => {
if (ready && authenticated) {
setModalContentStep("mainContent");
setIsMainModalOpen(true);
} else {
login();
}
};
const { login } = useLogin({
onComplete: ({ user, isNewUser, wasAlreadyAuthenticated, loginMethod, loginAccount }) => {
console.log("User logged in");
console.log(user, isNewUser, wasAlreadyAuthenticated, loginMethod, loginAccount);
handleOpenModal();
},
onError: (error) => {
console.log(error);
}
});
return /* @__PURE__ */ jsx(SwisstronikDigitalIdentity, { buttonCustomAction: handleOpenModal });
};
var SDIPrivyProvider = ({ children, config }) => {
let privyConfig;
if (config.privyConfig) {
privyConfig = config.privyConfig;
} else {
privyConfig = {
// Display email and wallet as login methods
// loginMethods: ['email', 'wallet'],
// Customize Privy's appearance in your app
appearance: {
theme: "light",
accentColor: "#0076EF",
logo: "https://swisstronik.com/assets/favicons/favicon-192.png",
walletChainType: "ethereum-only",
landingHeader: "Verify your identity"
},
walletConnectCloudProjectId: config.walletConnectProjectId,
// Create embedded wallets for users who don't have a wallet
embeddedWallets: {
createOnLogin: "users-without-wallets",
showWalletUIs: true,
extendedCalldataDecoding: true
// createOnLogin: 'off', // Anything other than 'off' will not be honored with whitelabel Auth. You must use createWallet from usePrivy()
// showWalletUIs: false,
},
defaultChain: swisstronikMainnetChain,
supportedChains: [swisstronikMainnetChain],
captchaEnabled: false
};
}
return /* @__PURE__ */ jsx(
PrivyProvider,
{
appId: config.privyAppId,
config: privyConfig,
children: /* @__PURE__ */ jsx(SDIContextProvider, { config, children })
}
);
};
export { SDIPrivyProvider, SwisstronikDigitalIdentityPrivy };