Skip to content

Commit a172405

Browse files
committed
Update
1 parent df8b2f6 commit a172405

File tree

2 files changed

+52
-4
lines changed
  • apps/dashboard/src/app

2 files changed

+52
-4
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from "@/constants/env";
1010
import { useThirdwebClient } from "@/constants/thirdweb.client";
1111
import { CustomConnectWallet } from "@3rdweb-sdk/react/components/connect-wallet";
12+
import { useAccount } from "@3rdweb-sdk/react/hooks/useApi";
1213
import { Turnstile } from "@marsidev/react-turnstile";
1314
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
1415
import type { CanClaimResponseType } from "app/api/testnet-faucet/can-claim/CanClaimResponseType";
@@ -133,6 +134,8 @@ export function FaucetButton({
133134

134135
const form = useForm<z.infer<typeof claimFaucetSchema>>();
135136

137+
const accountQuery = useAccount();
138+
136139
// loading state
137140
if (faucetWalletBalanceQuery.isPending || canClaimFaucetQuery.isPending) {
138141
return (
@@ -145,7 +148,7 @@ export function FaucetButton({
145148
// faucet is empty
146149
if (isFaucetEmpty) {
147150
return (
148-
<Button variant="outline" disabled className="!opacity-100 w-full ">
151+
<Button variant="outline" disabled className="!opacity-100 w-full">
149152
Faucet is empty right now
150153
</Button>
151154
);
@@ -168,10 +171,10 @@ export function FaucetButton({
168171
);
169172
}
170173

171-
if (!address) {
174+
if (!address || !accountQuery.data) {
172175
return (
173176
<CustomConnectWallet
174-
loginRequired={false}
177+
loginRequired={true}
175178
connectButtonClassName="!w-full !rounded !bg-primary !text-primary-foreground !px-4 !py-2 !text-sm"
176179
/>
177180
);

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { COOKIE_ACTIVE_ACCOUNT, COOKIE_PREFIX_TOKEN } from "@/constants/cookie";
2+
import { API_SERVER_URL } from "@/constants/env";
13
import { startOfToday } from "date-fns";
24
import { cacheGet, cacheSet } from "lib/redis";
35
import { type NextRequest, NextResponse } from "next/server";
4-
import { ZERO_ADDRESS } from "thirdweb";
6+
import { ZERO_ADDRESS, getAddress } from "thirdweb";
57
import { getFaucetClaimAmount } from "./claim-amount";
68

79
const THIRDWEB_ENGINE_URL = process.env.THIRDWEB_ENGINE_URL;
@@ -19,6 +21,49 @@ interface RequestTestnetFundsPayload {
1921

2022
// Note: This handler cannot use "edge" runtime because of Redis usage.
2123
export const POST = async (req: NextRequest) => {
24+
// Make sure user's connected to the site
25+
const activeAccount = req.cookies.get(COOKIE_ACTIVE_ACCOUNT)?.value;
26+
27+
if (!activeAccount) {
28+
return NextResponse.json(
29+
{
30+
error: "No account detected",
31+
},
32+
{ status: 400 },
33+
);
34+
}
35+
const authCookieName = COOKIE_PREFIX_TOKEN + getAddress(activeAccount);
36+
37+
const authCookie = req.cookies.get(authCookieName);
38+
39+
if (!authCookie) {
40+
return NextResponse.json(
41+
{
42+
error: "No wallet connected",
43+
},
44+
{ status: 400 },
45+
);
46+
}
47+
48+
// Make sure the connected wallet has a thirdweb account
49+
const accountRes = await fetch(`${API_SERVER_URL}/v1/account/me`, {
50+
method: "GET",
51+
headers: {
52+
Authorization: `Bearer ${authCookie.value}`,
53+
},
54+
});
55+
56+
if (accountRes.status !== 200) {
57+
// Account not found on this connected address
58+
return NextResponse.json(
59+
{
60+
error: "thirdweb account not found",
61+
},
62+
{ status: 400 },
63+
);
64+
}
65+
console.log(await accountRes.json());
66+
2267
const requestBody = (await req.json()) as RequestTestnetFundsPayload;
2368
const { chainId, toAddress, turnstileToken } = requestBody;
2469
if (Number.isNaN(chainId)) {

0 commit comments

Comments
 (0)