|
| 1 | +--- |
| 2 | +sidebar_label: Web3Auth SDK integration |
| 3 | +description: MetaMask + Web3Auth SDK Integration |
| 4 | +toc_max_heading_level: 2 |
| 5 | +--- |
| 6 | + |
| 7 | +# MetaMask SDK + Web3Auth SDK integration |
| 8 | + |
| 9 | +Get started with MetaMask SDK and [Web3Auth SDK](https://web3auth.io/docs/). |
| 10 | +You can set up the SDKs in the following ways: |
| 11 | + |
| 12 | +- [Quickstart template](#set-up-using-a-template) - Clone the template to set up a Next.js and Web3Auth dapp with both SDKs. |
| 13 | +- [Manual setup](#set-up-manually) - Set up both SDKs in an existing dapp. |
| 14 | + |
| 15 | +Features include: |
| 16 | + |
| 17 | +- **Dual SDK integration** - Seamlessly combine MetaMask and Web3Auth SDKs. |
| 18 | +- **Web3Auth social login** - Enable users to sign in with an email or social media account. |
| 19 | +- **Wallet connection** - Connect to MetaMask wallet with enhanced features. |
| 20 | +- **Mobile experience** - Optimized for both desktop and mobile users. |
| 21 | +- **TypeScript support** - Full type safety and modern development experience. |
| 22 | +- **Next.js integration** - Built with Next.js 15 and App Router. |
| 23 | + |
| 24 | +## Prerequisites |
| 25 | + |
| 26 | +- [Node.js](https://nodejs.org/) version 19 or later installed. |
| 27 | +- A package manager installed. |
| 28 | + The examples in this quickstart use [pnpm](https://pnpm.io/installation). |
| 29 | +- [MetaMask](https://metamask.io/) installed in your browser or on mobile. |
| 30 | +- A [Web3Auth Client ID](https://web3auth.io/docs/dashboard/create-new-project#get-the-client-id). |
| 31 | + |
| 32 | +## Set up using a template |
| 33 | + |
| 34 | +1. Download the [MetaMask SDK + Web3Auth SDK template](https://github.com/MetaMask/metamask-web3auth): |
| 35 | + |
| 36 | + ```bash |
| 37 | + git clone https://github.com/MetaMask/metamask-web3auth |
| 38 | + ``` |
| 39 | + |
| 40 | +2. Navigate into the repository: |
| 41 | + |
| 42 | + ```bash |
| 43 | + cd metamask-web3auth |
| 44 | + ``` |
| 45 | + |
| 46 | +3. Install dependencies: |
| 47 | + |
| 48 | + ```bash |
| 49 | + pnpm install |
| 50 | + ``` |
| 51 | + |
| 52 | +4. Create a `.env.local` file: |
| 53 | + |
| 54 | + ```bash |
| 55 | + touch .env.local |
| 56 | + ``` |
| 57 | + |
| 58 | +5. In `.env.local`, add a `NEXT_PUBLIC_WEB3AUTH_CLIENT_ID` environment variable, replacing `<YOUR-CLIENT-ID>` with your Web3Auth Client ID: |
| 59 | + |
| 60 | + ```text title=".env.local" |
| 61 | + NEXT_PUBLIC_WEB3AUTH_CLIENT_ID=<YOUR-CLIENT-ID> |
| 62 | + ``` |
| 63 | + |
| 64 | +6. Run the project: |
| 65 | + |
| 66 | + ```bash |
| 67 | + pnpm dev |
| 68 | + ``` |
| 69 | + |
| 70 | +You've successfully set up MetaMask SDK and Web3Auth SDK. |
| 71 | +See how to [use the combined SDKs](#usage). |
| 72 | +
|
| 73 | +## Set up manually |
| 74 | +
|
| 75 | +### 1. Install dependencies |
| 76 | +
|
| 77 | +Install the SDK and the required dependencies to an existing project: |
| 78 | +
|
| 79 | +```bash |
| 80 | +pnpm i viem wagmi @tanstack/react-query @web3auth/modal@10 |
| 81 | +``` |
| 82 | +
|
| 83 | +### 2. Configure providers |
| 84 | +
|
| 85 | +Set up your providers in `app/providers.tsx`: |
| 86 | +
|
| 87 | +```typescript title="providers.tsx" |
| 88 | +"use client"; |
| 89 | +
|
| 90 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 91 | +import { type ReactNode, useState, useEffect } from "react"; |
| 92 | +import { Web3AuthProvider } from "@web3auth/modal/react"; |
| 93 | +import { WagmiProvider } from "@web3auth/modal/react/wagmi"; |
| 94 | +import { MetaMaskSDK } from "@metamask/sdk"; |
| 95 | +
|
| 96 | +type Props = { |
| 97 | + children: ReactNode; |
| 98 | +}; |
| 99 | +
|
| 100 | +export function Providers({ children }: Props) { |
| 101 | + const [queryClient] = useState(() => new QueryClient()); |
| 102 | +
|
| 103 | + useEffect(() => { |
| 104 | + if (typeof window === "undefined") return; |
| 105 | +
|
| 106 | + const MMSDK = new MetaMaskSDK({ |
| 107 | + dappMetadata: { |
| 108 | + name: "MetaMask Web3Auth Integration", |
| 109 | + url: window.location.href, |
| 110 | + }, |
| 111 | + }); |
| 112 | +
|
| 113 | + const ethereum = MMSDK.getProvider(); |
| 114 | + if (ethereum) { |
| 115 | + window.ethereum = ethereum as unknown as IEthereum; |
| 116 | + } |
| 117 | + }, []); |
| 118 | +
|
| 119 | + return ( |
| 120 | + <Web3AuthProvider |
| 121 | + config={{ |
| 122 | + web3AuthOptions: { |
| 123 | + clientId: process.env.NEXT_PUBLIC_WEB3AUTH_CLIENT_ID!, |
| 124 | + web3AuthNetwork: "sapphire_devnet" |
| 125 | + }, |
| 126 | + }} |
| 127 | + > |
| 128 | + <QueryClientProvider client={queryClient}> |
| 129 | + <WagmiProvider> |
| 130 | + <div className="container">{children}</div> |
| 131 | + </WagmiProvider> |
| 132 | + </QueryClientProvider> |
| 133 | + </Web3AuthProvider> |
| 134 | + ); |
| 135 | +} |
| 136 | +``` |
| 137 | +
|
| 138 | +### 3. Set up environment variables |
| 139 | +
|
| 140 | +Create a `.env.local` file. |
| 141 | +In `.env.local`, add a `NEXT_PUBLIC_WEB3AUTH_CLIENT_ID` environment variable, replacing `<YOUR-CLIENT-ID>` with your Web3Auth Client ID: |
| 142 | +
|
| 143 | +```text title=".env.local" |
| 144 | +NEXT_PUBLIC_WEB3AUTH_CLIENT_ID=<YOUR-CLIENT-ID> |
| 145 | +``` |
| 146 | +
|
| 147 | +You can now test your dapp by running `pnpm run dev`. |
| 148 | +
|
| 149 | +## Usage |
| 150 | +
|
| 151 | +### Connect or sign in |
| 152 | +
|
| 153 | +Use the `useWeb3AuthConnect` hook to enable users to connect or sign in to their wallet: |
| 154 | +
|
| 155 | +```typescript |
| 156 | +"use client"; |
| 157 | +
|
| 158 | +import { useWeb3AuthConnect } from "@web3auth/modal/react"; |
| 159 | +
|
| 160 | +export const Navbar = () => { |
| 161 | + const { connect } = useWeb3AuthConnect(); |
| 162 | +
|
| 163 | + return ( |
| 164 | + <nav> |
| 165 | + <button onClick={() => connect()}>Connect or Sign in</button>; |
| 166 | + </nav> |
| 167 | + ); |
| 168 | +}; |
| 169 | +``` |
| 170 | +
|
| 171 | +### Check wallet status |
| 172 | +
|
| 173 | +Use the `useAccount` hook from Wagmi to check the wallet status: |
| 174 | +
|
| 175 | +```typescript |
| 176 | +"use client"; |
| 177 | +
|
| 178 | +import { useAccount } from "wagmi"; |
| 179 | +
|
| 180 | +export const Hero = () => { |
| 181 | + const { address, isConnected } = useAccount(); |
| 182 | +
|
| 183 | + return ( |
| 184 | + <div> |
| 185 | + {isConnected ? <p>Connected: {address}</p> : <p>Not connected</p>} |
| 186 | + </div> |
| 187 | + ); |
| 188 | +}; |
| 189 | +``` |
| 190 | +
|
| 191 | +### Send a transaction |
| 192 | +
|
| 193 | +Use the `useSendTransaction` hook from Wagmi to send a transaction: |
| 194 | +
|
| 195 | +```typescript |
| 196 | +"use client"; |
| 197 | +
|
| 198 | +import { useSendTransaction } from "wagmi"; |
| 199 | +import { parseEther } from "viem"; |
| 200 | +
|
| 201 | +export const SendTransaction = () => { |
| 202 | + const { sendTransaction } = useSendTransaction(); |
| 203 | +
|
| 204 | + return ( |
| 205 | + <button |
| 206 | + onClick={() => |
| 207 | + sendTransaction({ |
| 208 | + to: "0xd2135CfB216b74109775236E36d4b433F1DF507B", |
| 209 | + value: parseEther("0.001"), |
| 210 | + }) |
| 211 | + } |
| 212 | + > |
| 213 | + Send transaction |
| 214 | + </button> |
| 215 | + ); |
| 216 | +}; |
| 217 | +``` |
0 commit comments