Skip to content

Commit 3fd64e0

Browse files
committed
Add Web3Auth SDK quickstart
1 parent 47a160f commit 3fd64e0

File tree

2 files changed

+219
-0
lines changed

2 files changed

+219
-0
lines changed

sdk-sidebar.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const sidebar = {
2929
'quickstart/javascript-wagmi',
3030
'quickstart/javascript',
3131
'quickstart/javascript-dynamic',
32+
'quickstart/javascript-web3auth',
3233
'quickstart/react-native',
3334
],
3435
},
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
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 Wagmi 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
27+
- [pnpm](https://pnpm.io/installation)
28+
- [MetaMask](https://metamask.io/) installed in your browser or on mobile
29+
- A [Web3Auth Client ID](https://web3auth.io/docs/dashboard-setup/projects-and-analytics#client-id)
30+
31+
## Set up using a template
32+
33+
1. Download the template from the
34+
<a href="https://github.com/metamask/metamask-sdk-examples" target="_blank">SDK examples repository</a>:
35+
36+
```bash
37+
git clone https://github.com/metamask/metamask-sdk-examples.git
38+
```
39+
40+
2. Navigate into the Web3Auth example:
41+
42+
```bash
43+
cd metamask-sdk-examples/examples/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/[email protected]
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 } 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+
authBuildEnv: "testing", // Optional: Only for alpha/testing
126+
},
127+
}}
128+
>
129+
<QueryClientProvider client={queryClient}>
130+
<WagmiProvider>
131+
<div className="container">{children}</div>
132+
</WagmiProvider>
133+
</QueryClientProvider>
134+
</Web3AuthProvider>
135+
);
136+
}
137+
```
138+
139+
### 3. Set up environment variables
140+
141+
Create a `.env.local` file.
142+
In `.env.local`, add a `NEXT_PUBLIC_WEB3AUTH_CLIENT_ID` environment variable, replacing `<YOUR-CLIENT-ID>` with your Web3Auth Client ID:
143+
144+
```text title=".env.local"
145+
NEXT_PUBLIC_WEB3AUTH_CLIENT_ID=<YOUR-CLIENT-ID>
146+
```
147+
148+
You can now test your dapp by running `pnpm run dev`.
149+
150+
## Usage
151+
152+
### Connect or sign in
153+
154+
Use the `useWeb3AuthConnect` hook to enable users to connect or sign in to their wallet:
155+
156+
```typescript
157+
"use client";
158+
159+
import { useWeb3AuthConnect } from "@web3auth/modal/react";
160+
161+
export const Navbar = () => {
162+
const { connect } = useWeb3AuthConnect();
163+
164+
return (
165+
<nav>
166+
<button onClick={() => connect()}>Connect or Sign in</button>;
167+
</nav>
168+
);
169+
};
170+
```
171+
172+
### Check wallet status
173+
174+
Use the `useAccount` hook from Wagmi to check the wallet status:
175+
176+
```typescript
177+
"use client";
178+
179+
import { useAccount } from "wagmi";
180+
181+
export const Hero = () => {
182+
const { address, isConnected } = useAccount();
183+
184+
return (
185+
<div>
186+
{isConnected ? <p>Connected: {address}</p> : <p>Not connected</p>}
187+
</div>
188+
);
189+
};
190+
```
191+
192+
### Send a transaction
193+
194+
Use the `useSendTransaction` hook from Wagmi to send a transaction:
195+
196+
```typescript
197+
"use client";
198+
199+
import { useSendTransaction } from "wagmi";
200+
import { parseEther } from "viem";
201+
202+
export const SendTransaction = () => {
203+
const { sendTransaction } = useSendTransaction();
204+
205+
return (
206+
<button
207+
onClick={() =>
208+
sendTransaction({
209+
to: "0xd2135CfB216b74109775236E36d4b433F1DF507B",
210+
value: parseEther("0.001"),
211+
})
212+
}
213+
>
214+
Send transaction
215+
</button>
216+
);
217+
};
218+
```

0 commit comments

Comments
 (0)