|
| 1 | +--- |
| 2 | +title: Integrating Rainbow Kit with Web3Auth PnP SDK for Authentication |
| 3 | +image: "guides/banners/rainbowkit-guide.png" |
| 4 | +description: Learn how to integrate Rainbowkit and Web3auth for authentication. |
| 5 | +type: guide |
| 6 | +tags: [rainbowkit, authentication, pnp, wagmi] |
| 7 | +date: March 13, 2024 |
| 8 | +author: Web3Auth Team |
| 9 | +order: 5 |
| 10 | +communityPortalTopicId: |
| 11 | +--- |
| 12 | + |
| 13 | +import SEO from "@site/src/components/SEO"; |
| 14 | +import SetupWeb3AuthDashboard from "@site/src/common/guides/_setup-web3auth-dashboard.mdx"; |
| 15 | +import Web3AuthPrerequisites from "@site/src/common/guides/_web3auth-prerequisites.mdx"; |
| 16 | +import DashboardSetup from "@site/src/common/guides/_web3auth_dashboard_setup.mdx"; |
| 17 | + |
| 18 | +<SEO |
| 19 | + title="Integrating RainbowKit with Web3Auth for Authentication" |
| 20 | + description="Learn how to integrate Rainbowkit and Web3auth for authentication." |
| 21 | + image="https://web3auth.io/docs/guides/banners/rainbowkit-guide.png" |
| 22 | + slug="/guides/rainbow-kit-guide" |
| 23 | +/> |
| 24 | + |
| 25 | +Merging RainbowKit with Web3Auth PnP (Plug and Play) SDK enables decentralized application (dApp) |
| 26 | +developers to leverage Web3Auth's streamlined authentication processes alongside RainbowKit's |
| 27 | +user-friendly wallet management interface. This integration utilizes Web3Auth's |
| 28 | +[@web3auth/web3auth-wagmi-connector](https://web3auth.io/docs/sdk/pnp/web/wagmi-connector) to bridge |
| 29 | +RainbowKit with Web3Auth, facilitating a secure and efficient authentication mechanism for users. |
| 30 | + |
| 31 | +## Full examples in PnP SDK |
| 32 | + |
| 33 | +Full Modal example: |
| 34 | +https://github.com/Web3Auth/web3auth-pnp-examples/tree/main/web-modal-sdk/wagmi-examples/rainbowkit-modal-example |
| 35 | + |
| 36 | +Full No-Modal example: |
| 37 | +https://github.com/Web3Auth/web3auth-pnp-examples/tree/main/web-no-modal-sdk/wagmi/rainbowkit-no-modal-example |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +## Prerequisites |
| 42 | + |
| 43 | +<Web3AuthPrerequisites /> |
| 44 | + |
| 45 | +## How to set up Web3Auth Dashboard |
| 46 | + |
| 47 | +<DashboardSetup /> |
| 48 | + |
| 49 | +## Rainbow + Web3Auth Connector |
| 50 | + |
| 51 | +Using rainbow kit with web3auth is very straightforward. You will need to create a connector that |
| 52 | +will connect the rainbow kit with web3auth. This connector will be used in the wagmi provider. |
| 53 | + |
| 54 | +You will need to use mainly 8 libraries in this project: `@web3auth/web3auth-wagmi-connector`, |
| 55 | +`@rainbow-me/rainbowkit` and `@web3auth/modal` or `@web3auth/no-modal`. Also we would need in the |
| 56 | +project `@web3auth/base`, "@web3auth/ethereum-provider", `viem`, `wagmi` and |
| 57 | +`@web3auth/openlogin-adapter`. |
| 58 | + |
| 59 | +To install them, run: |
| 60 | + |
| 61 | +```bash |
| 62 | +npm install @web3auth/web3auth-wagmi-connector @rainbow-me/rainbowkit @web3auth/modal @web3auth/base @web3auth/ethereum-provider @web3auth/openlogin-adapter |
| 63 | +``` |
| 64 | + |
| 65 | +\* you can replace modal with no-modal |
| 66 | + |
| 67 | +The rainbow connector is the key that connect rainbowkit with web3auth. So in this file you will be |
| 68 | +exporting the connector with the web3auth instance on it. |
| 69 | + |
| 70 | +```jsx |
| 71 | +import { Web3AuthConnector } from "@web3auth/web3auth-wagmi-connector"; |
| 72 | + |
| 73 | +... |
| 74 | + |
| 75 | +const web3AuthInstance = new Web3Auth({ |
| 76 | + clientId, |
| 77 | + web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET, |
| 78 | + privateKeyProvider, |
| 79 | + ... // all the config you need |
| 80 | + } |
| 81 | +}); |
| 82 | + |
| 83 | +export const rainbowWeb3AuthConnector = (): Wallet => ({ |
| 84 | + id: "web3auth", |
| 85 | + name: "web3auth", |
| 86 | + rdns: "web3auth", |
| 87 | + iconUrl: "https://web3auth.io/images/web3authlog.png", |
| 88 | + iconBackground: "#fff", |
| 89 | + installed: true, |
| 90 | + downloadUrls: {}, |
| 91 | + createConnector: (walletDetails: WalletDetailsParams) => |
| 92 | + createWagmiConnector((config) => ({ |
| 93 | + ...Web3AuthConnector({ |
| 94 | + web3AuthInstance, |
| 95 | + })(config), |
| 96 | + ...walletDetails, |
| 97 | + })), |
| 98 | +}); |
| 99 | +``` |
| 100 | + |
| 101 | +## Implementing Chains and Providers |
| 102 | + |
| 103 | +To ensure that your dApp supports various blockchain networks, you'll need to import and configure |
| 104 | +the chains you intend to use, such as Ethereum's Mainnet, Polygon, or others. This step is crucial |
| 105 | +for setting up the environment for transactions and interactions with the blockchain. |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +```jsx |
| 110 | +import { sepolia, mainnet, polygon } from "wagmi/chains"; |
| 111 | + |
| 112 | +... |
| 113 | + |
| 114 | +const config = getDefaultConfig({ |
| 115 | + appName: 'My RainbowKit App Name', |
| 116 | + projectId: '<code>', |
| 117 | + chains: [mainnet, sepolia, polygon], |
| 118 | + transports: { |
| 119 | + [mainnet.id]: http(), |
| 120 | + [sepolia.id]: http(), |
| 121 | + [polygon.id]: http(), |
| 122 | + }, |
| 123 | + wallets: [{ |
| 124 | + groupName: 'Recommended', |
| 125 | + wallets: [ |
| 126 | + rainbowWallet, |
| 127 | + rainbowWeb3AuthConnector, |
| 128 | + metaMaskWallet, |
| 129 | + ], |
| 130 | + }], |
| 131 | +}); |
| 132 | +``` |
| 133 | + |
| 134 | +## Integrating with the UI |
| 135 | + |
| 136 | +In the initial react screen you will need to create the wagmi provider with the QueryClientProvider |
| 137 | +and the RainbowKitProvider inside. |
| 138 | + |
| 139 | +The final step involves integrating the RainbowKitProvider and ConnectButton into your application's |
| 140 | +UI. This is typically done at the root level of your application to ensure that the wallet |
| 141 | +connection functionality is accessible throughout the dApp. The WagmiProvider and |
| 142 | +QueryClientProvider wrap around the RainbowKit components, establishing the necessary context for |
| 143 | +managing blockchain interactions and state. |
| 144 | + |
| 145 | +```jsx |
| 146 | +<WagmiProvider config={config}> |
| 147 | + <QueryClientProvider client={queryClient}> |
| 148 | + <RainbowKitProvider> |
| 149 | + <ConnectButton /> |
| 150 | + </RainbowKitProvider> |
| 151 | + </QueryClientProvider> |
| 152 | +</WagmiProvider> |
| 153 | +``` |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | +## Conclusion |
| 158 | + |
| 159 | +If you're using RainbowKit in your dApp, integrating Web3Auth is incredibly straightforward and |
| 160 | +enhances your application's user experience significantly. The seamless integration process means |
| 161 | +that you can provide your users with a more secure and efficient authentication mechanism without |
| 162 | +compromising on the user-friendly interface that RainbowKit offers. |
| 163 | + |
| 164 | +RainbowKit, known for its ease of wallet management, when combined with Web3Auth's authentication |
| 165 | +solutions, essentially offers the best of both worlds. Web3Auth, with its Plug and Play (PnP) SDK, |
| 166 | +simplifies the complex authentication processes often associated with decentralized applications. |
| 167 | +This means that even developers with a basic understanding of JavaScript can integrate sophisticated |
| 168 | +authentication mechanisms into their dApps. |
0 commit comments