Skip to content

Commit 3d8115a

Browse files
committed
Update
1 parent f40d247 commit 3d8115a

File tree

2 files changed

+100
-11
lines changed
  • apps/dashboard/src/app

2 files changed

+100
-11
lines changed

apps/dashboard/src/app/(dashboard)/(chain)/[chain_id]/(chainPage)/components/client/FaucetButton.tsx

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use client";
22

3+
import { ChakraProviderSetup } from "@/components/ChakraProviderSetup";
34
import { Spinner } from "@/components/ui/Spinner/Spinner";
45
import { Button } from "@/components/ui/button";
56
import { Form, FormControl, FormField, FormItem } from "@/components/ui/form";
@@ -9,11 +10,14 @@ import {
910
} from "@/constants/env";
1011
import { useThirdwebClient } from "@/constants/thirdweb.client";
1112
import { CustomConnectWallet } from "@3rdweb-sdk/react/components/connect-wallet";
13+
import { useAccount } from "@3rdweb-sdk/react/hooks/useApi";
1214
import { Turnstile } from "@marsidev/react-turnstile";
1315
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
1416
import type { CanClaimResponseType } from "app/api/testnet-faucet/can-claim/CanClaimResponseType";
17+
import { Onboarding } from "components/onboarding";
1518
import { mapV4ChainToV5Chain } from "contexts/map-chains";
1619
import { useTrack } from "hooks/analytics/useTrack";
20+
import { useState } from "react";
1721
import { useForm } from "react-hook-form";
1822
import { toast } from "sonner";
1923
import { toUnits } from "thirdweb";
@@ -133,6 +137,9 @@ export function FaucetButton({
133137

134138
const form = useForm<z.infer<typeof claimFaucetSchema>>();
135139

140+
const accountQuery = useAccount();
141+
const [showOnboarding, setShowOnBoarding] = useState(false);
142+
136143
// loading state
137144
if (faucetWalletBalanceQuery.isPending || canClaimFaucetQuery.isPending) {
138145
return (
@@ -145,7 +152,7 @@ export function FaucetButton({
145152
// faucet is empty
146153
if (isFaucetEmpty) {
147154
return (
148-
<Button variant="outline" disabled className="!opacity-100 w-full ">
155+
<Button variant="outline" disabled className="!opacity-100 w-full">
149156
Faucet is empty right now
150157
</Button>
151158
);
@@ -168,15 +175,40 @@ export function FaucetButton({
168175
);
169176
}
170177

171-
if (!address) {
178+
if (!address || !accountQuery.data) {
172179
return (
173180
<CustomConnectWallet
174-
loginRequired={false}
181+
loginRequired={true}
175182
connectButtonClassName="!w-full !rounded !bg-primary !text-primary-foreground !px-4 !py-2 !text-sm"
176183
/>
177184
);
178185
}
179186

187+
// Email verification is required to claim from the faucet
188+
// same logic with the Onbrading state
189+
if (
190+
!accountQuery.data.emailConfirmedAt &&
191+
!accountQuery.data.unconfirmedEmail
192+
) {
193+
return (
194+
<>
195+
<Button
196+
variant="outline"
197+
className="!opacity-100 w-full"
198+
onClick={() => setShowOnBoarding(true)}
199+
>
200+
Verify your email
201+
</Button>
202+
{/* We will show the modal only if the user click on it, because this is a public page */}
203+
{showOnboarding && (
204+
<ChakraProviderSetup>
205+
<Onboarding />
206+
</ChakraProviderSetup>
207+
)}
208+
</>
209+
);
210+
}
211+
180212
const claimFunds = (values: z.infer<typeof claimFaucetSchema>) => {
181213
// Instead of having a dedicated endpoint (/api/verify-token),
182214
// we can just attach the token in the payload and send it to the claim-faucet endpoint, to avoid a round-trip request

apps/dashboard/src/app/api/testnet-faucet/claim/route.ts

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { COOKIE_ACTIVE_ACCOUNT, COOKIE_PREFIX_TOKEN } from "@/constants/cookie";
2+
import { API_SERVER_URL } from "@/constants/env";
3+
import type { Account } from "@3rdweb-sdk/react/hooks/useApi";
14
import { startOfToday } from "date-fns";
25
import { cacheGet, cacheSet } from "lib/redis";
36
import { type NextRequest, NextResponse } from "next/server";
4-
import { ZERO_ADDRESS } from "thirdweb";
7+
import { ZERO_ADDRESS, getAddress } from "thirdweb";
58
import { getFaucetClaimAmount } from "./claim-amount";
69

710
const THIRDWEB_ENGINE_URL = process.env.THIRDWEB_ENGINE_URL;
@@ -19,6 +22,60 @@ interface RequestTestnetFundsPayload {
1922

2023
// Note: This handler cannot use "edge" runtime because of Redis usage.
2124
export const POST = async (req: NextRequest) => {
25+
// Make sure user's connected to the site
26+
const activeAccount = req.cookies.get(COOKIE_ACTIVE_ACCOUNT)?.value;
27+
28+
if (!activeAccount) {
29+
return NextResponse.json(
30+
{
31+
error: "No wallet detected",
32+
},
33+
{ status: 400 },
34+
);
35+
}
36+
const authCookieName = COOKIE_PREFIX_TOKEN + getAddress(activeAccount);
37+
38+
const authCookie = req.cookies.get(authCookieName);
39+
40+
if (!authCookie) {
41+
return NextResponse.json(
42+
{
43+
error: "No wallet connected",
44+
},
45+
{ status: 400 },
46+
);
47+
}
48+
49+
// Make sure the connected wallet has a thirdweb account
50+
const accountRes = await fetch(`${API_SERVER_URL}/v1/account/me`, {
51+
method: "GET",
52+
headers: {
53+
Authorization: `Bearer ${authCookie.value}`,
54+
},
55+
});
56+
57+
if (accountRes.status !== 200) {
58+
// Account not found on this connected address
59+
return NextResponse.json(
60+
{
61+
error: "thirdweb account not found",
62+
},
63+
{ status: 400 },
64+
);
65+
}
66+
67+
const account: Account = await accountRes.json();
68+
69+
// Make sure the logged-in account has verified its email
70+
if (!account.emailConfirmedAt && !account.unconfirmedEmail) {
71+
return NextResponse.json(
72+
{
73+
error: "Account owner hasn't verified email",
74+
},
75+
{ status: 400 },
76+
);
77+
}
78+
2279
const requestBody = (await req.json()) as RequestTestnetFundsPayload;
2380
const { chainId, toAddress, turnstileToken } = requestBody;
2481
if (Number.isNaN(chainId)) {
@@ -86,17 +143,17 @@ export const POST = async (req: NextRequest) => {
86143
);
87144
}
88145

89-
const ipCacheKey = `testnet-faucet:${chainId}:${ipAddress}`;
90146
const addressCacheKey = `testnet-faucet:${chainId}:${toAddress}`;
147+
const accountCacheKey = `testnet-faucet:${chainId}:${account.id}`;
91148

92-
// Assert 1 request per IP/chain every 24 hours.
149+
// Assert 1 request per userId every 24 hours.
93150
// get the cached value
94-
const [ipCacheValue, addressCache] = await Promise.all([
95-
cacheGet(ipCacheKey),
151+
const [accountCacheValue, addressCache] = await Promise.all([
152+
cacheGet(accountCacheKey),
96153
cacheGet(addressCacheKey),
97154
]);
98155
// if we have a cached value, return an error
99-
if (ipCacheValue !== null || addressCache !== null) {
156+
if (accountCacheValue !== null || addressCache !== null) {
100157
return NextResponse.json(
101158
{ error: "Already requested funds on this chain in the past 24 hours." },
102159
{ status: 429 },
@@ -109,13 +166,13 @@ export const POST = async (req: NextRequest) => {
109166
todayLocal.getTime() - todayLocal.getTimezoneOffset() * 60000,
110167
);
111168
const todayUTCSeconds = Math.floor(todayUTC.getTime() / 1000);
112-
const idempotencyKey = `${ipCacheKey}:${todayUTCSeconds}`;
169+
const idempotencyKey = `${accountCacheValue}:${todayUTCSeconds}`;
113170
const amountToClaim = getFaucetClaimAmount(chainId).toString();
114171

115172
try {
116173
// Store the claim request for 24 hours.
117174
await Promise.all([
118-
cacheSet(ipCacheKey, "claimed", 24 * 60 * 60),
175+
cacheSet(accountCacheKey, "claimed", 24 * 60 * 60),
119176
cacheSet(addressCacheKey, "claimed", 24 * 60 * 60),
120177
]);
121178
// then actually transfer the funds

0 commit comments

Comments
 (0)