Skip to content

Commit 7b0a27e

Browse files
alexandratranshahbaz17bgravenorst
authored
Add Web3Auth SDK quickstart (#2029)
* Add Web3Auth SDK quickstart * Update what's new * address reviewer comments * address reviewer comments * link + example repo changes --------- Co-authored-by: Mohammad Shahbaz Alam <[email protected]> Co-authored-by: Byron Gravenorst <[email protected]>
1 parent 9ff9a32 commit 7b0a27e

File tree

5 files changed

+229
-7
lines changed

5 files changed

+229
-7
lines changed

docs/whats-new.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ of the [MetaMask developer page](https://metamask.io/developer/).
1111

1212
## May 2025
1313

14+
- Documented [MetaMask SDK + Web3Auth SDK integration](/sdk/quickstart/javascript-web3auth).
15+
([#2029](https://github.com/MetaMask/metamask-docs/pull/2029))
1416
- Documented [how to use the Snaps sandbox](/snaps/how-to/test-a-snap/#test-in-the-sandbox).
1517
([#2030](https://github.com/MetaMask/metamask-docs/pull/2030))
1618
- Documented how to use the SDK CLI to set up a [JavaScript + Wagmi](/sdk/quickstart/javascript-wagmi) or [Dynamic SDK](/sdk/quickstart/javascript-dynamic) project.

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
},

sdk/quickstart/javascript-dynamic.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ The project you will set up has the following structure:
4343

4444
## Prerequisites
4545

46-
- [Node.js](https://nodejs.org/) version 19 or later
47-
- [pnpm](https://pnpm.io/installation)
48-
- [MetaMask](https://metamask.io/) installed in your browser or on mobile
49-
- A [Dynamic Environment ID](https://app.dynamic.xyz/)
46+
- [Node.js](https://nodejs.org/) version 19 or later installed.
47+
- A package manager installed.
48+
The examples in this quickstart use [pnpm](https://pnpm.io/installation).
49+
- [MetaMask](https://metamask.io/) installed in your browser or on mobile.
50+
- A [Dynamic Environment ID](https://app.dynamic.xyz/).
5051

5152
## Set up using the CLI
5253

sdk/quickstart/javascript-wagmi.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ The project you will set up has the following structure:
4343

4444
## Prerequisites
4545

46-
- [Node.js](https://nodejs.org/) version 19 or later
47-
- [pnpm](https://pnpm.io/installation)
48-
- [MetaMask](https://metamask.io/) installed in your browser or on mobile
46+
- [Node.js](https://nodejs.org/) version 19 or later installed.
47+
- A package manager installed.
48+
The examples in this quickstart use [pnpm](https://pnpm.io/installation).
49+
- [MetaMask](https://metamask.io/) installed in your browser or on mobile.
4950

5051
## Set up using the CLI
5152

sdk/quickstart/javascript-web3auth.md

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
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

Comments
 (0)