Skip to content

Commit 5848e25

Browse files
committed
feat: add custom auth demo in playground
1 parent 3488281 commit 5848e25

File tree

2 files changed

+146
-7
lines changed

2 files changed

+146
-7
lines changed

apps/playground-web/src/app/connect/in-app-wallet/page.tsx

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CodeExample } from "@/components/code/code-example";
2+
import { CustomLoginForm } from "@/components/in-app-wallet/custom-login-form";
23
import { InAppConnectEmbed } from "../../../components/in-app-wallet/connect-button";
34
import { Profiles } from "../../../components/in-app-wallet/profile-sections";
45
import ThirdwebProvider from "../../../components/thirdweb-provider";
@@ -30,13 +31,13 @@ function AnyAuth() {
3031
base.
3132
</p>
3233
</div>
33-
34-
<CodeExample
35-
preview={<InAppConnectEmbed />}
36-
code={`import { inAppWallet } from "thirdweb/wallets";
34+
<div className="space-y-2">
35+
<h3 className="mb-3 font-medium text-lg">Prebuilt UI</h3>
36+
<CodeExample
37+
preview={<InAppConnectEmbed />}
38+
code={`import { inAppWallet } from "thirdweb/wallets";
3739
import { ConnectEmbed } from "thirdweb/react";
3840
39-
4041
const wallets = [
4142
inAppWallet(
4243
// built-in auth methods
@@ -66,8 +67,79 @@ function AnyAuth() {
6667
return (
6768
<ConnectEmbed client={client} wallets={wallets} />);
6869
};`}
69-
lang="tsx"
70-
/>
70+
lang="tsx"
71+
/>
72+
</div>
73+
<div className="space-y-2">
74+
<h3 className="mb-3 font-medium text-lg">Custom Auth and UI</h3>
75+
<CodeExample
76+
preview={
77+
<div className="w-1/2">
78+
<CustomLoginForm />
79+
</div>
80+
}
81+
code={`import { useMutation } from "@tanstack/react-query";
82+
import { useState } from "react";
83+
import { useConnect } from "thirdweb/react";
84+
import { inAppWallet } from "thirdweb/wallets";
85+
86+
export function CustomLoginForm() {
87+
const [email, setEmail] = useState("");
88+
const { connect, isConnecting, error } = useConnect();
89+
90+
const { mutate: loginWithCustomAuth } = useMutation({
91+
mutationFn: async (email: string) => {
92+
const wallet = await connect(async () => {
93+
const wallet = inAppWallet();
94+
await wallet.connect({
95+
strategy: "auth_endpoint",
96+
client,
97+
// your own custom auth payload here
98+
payload: JSON.stringify({
99+
userId: email,
100+
email,
101+
}),
102+
});
103+
return wallet;
104+
});
105+
return wallet;
106+
}
107+
});
108+
109+
const handleSubmit = (e: React.FormEvent) => {
110+
e.preventDefault();
111+
loginWithCustomAuth(email);
112+
};
113+
114+
return (
115+
<form onSubmit={handleSubmit}>
116+
<div>
117+
<label htmlFor="email">
118+
Email Address
119+
</label>
120+
<input
121+
type="email"
122+
id="email"
123+
value={email}
124+
onChange={(e) => setEmail(e.target.value)}
125+
placeholder="Enter your email"
126+
required
127+
/>
128+
<button
129+
type="submit"
130+
disabled={isConnecting || !email}
131+
>
132+
{isConnecting ? "Submitting..." : "Submit"}
133+
</button>
134+
{error && <p>{error.message}</p>}
135+
</div>
136+
</form>
137+
);
138+
}
139+
`}
140+
lang="tsx"
141+
/>
142+
</div>
71143
</>
72144
);
73145
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"use client";
2+
3+
import { THIRDWEB_CLIENT } from "@/lib/client";
4+
import { useMutation } from "@tanstack/react-query";
5+
import { useState } from "react";
6+
import { useActiveAccount, useConnect } from "thirdweb/react";
7+
import { inAppWallet } from "thirdweb/wallets";
8+
import { InAppConnectEmbed } from "./connect-button";
9+
10+
export function CustomLoginForm() {
11+
const [email, setEmail] = useState("");
12+
const { connect, isConnecting, error } = useConnect();
13+
const account = useActiveAccount();
14+
15+
const { mutate: loginWithCustomAuthEndpoint } = useMutation({
16+
mutationFn: async (email: string) => {
17+
const wallet = await connect(async () => {
18+
const wallet = inAppWallet();
19+
await wallet.connect({
20+
strategy: "auth_endpoint",
21+
client: THIRDWEB_CLIENT,
22+
payload: JSON.stringify({
23+
userId: email,
24+
email,
25+
}),
26+
});
27+
return wallet;
28+
});
29+
return wallet;
30+
},
31+
});
32+
33+
const handleSubmit = (e: React.FormEvent) => {
34+
e.preventDefault();
35+
loginWithCustomAuthEndpoint(email);
36+
};
37+
if (account) {
38+
return <InAppConnectEmbed />;
39+
}
40+
41+
return (
42+
<form onSubmit={handleSubmit} className="mt-4">
43+
<div className="flex flex-col space-y-2">
44+
<label htmlFor="email" className="font-medium text-sm">
45+
Email Address
46+
</label>
47+
<input
48+
type="email"
49+
id="email"
50+
value={email}
51+
onChange={(e) => setEmail(e.target.value)}
52+
className="rounded-lg border px-3 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500"
53+
placeholder="Enter your email"
54+
required
55+
/>
56+
<button
57+
type="submit"
58+
className="rounded-lg bg-blue-500 px-4 py-2 text-white transition-colors enabled:hover:bg-blue-600"
59+
disabled={isConnecting || !email}
60+
>
61+
{isConnecting ? "Submitting..." : "Submit"}
62+
</button>
63+
{error && <p className="max-w-[300px] text-red-500">{error.message}</p>}
64+
</div>
65+
</form>
66+
);
67+
}

0 commit comments

Comments
 (0)