From 89a6e1c64431e33a0a7416ee9bb491d1d1a301a7 Mon Sep 17 00:00:00 2001 From: Kien Ngo Date: Wed, 30 Oct 2024 06:20:41 +0700 Subject: [PATCH] [Dashboard] Improve faucet claim --- .../src/app/api/testnet-faucet/claim/route.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/apps/dashboard/src/app/api/testnet-faucet/claim/route.ts b/apps/dashboard/src/app/api/testnet-faucet/claim/route.ts index 076b549d7e3..6a5ce3d2f0e 100644 --- a/apps/dashboard/src/app/api/testnet-faucet/claim/route.ts +++ b/apps/dashboard/src/app/api/testnet-faucet/claim/route.ts @@ -1,7 +1,8 @@ +import { COOKIE_ACTIVE_ACCOUNT, COOKIE_PREFIX_TOKEN } from "@/constants/cookie"; import { startOfToday } from "date-fns"; import { cacheGet, cacheSet } from "lib/redis"; import { type NextRequest, NextResponse } from "next/server"; -import { ZERO_ADDRESS } from "thirdweb"; +import { ZERO_ADDRESS, getAddress } from "thirdweb"; import { getFaucetClaimAmount } from "./claim-amount"; const THIRDWEB_ENGINE_URL = process.env.THIRDWEB_ENGINE_URL; @@ -19,6 +20,20 @@ interface RequestTestnetFundsPayload { // Note: This handler cannot use "edge" runtime because of Redis usage. export const POST = async (req: NextRequest) => { + // Make sure user's connected to the site + const activeAccount = req.cookies.get(COOKIE_ACTIVE_ACCOUNT)?.value; + const authCookie = activeAccount + ? req.cookies.get(COOKIE_PREFIX_TOKEN + getAddress(activeAccount)) + : null; + if (!authCookie) { + return NextResponse.json( + { + error: "No wallet connected", + }, + { status: 400 }, + ); + } + const requestBody = (await req.json()) as RequestTestnetFundsPayload; const { chainId, toAddress, turnstileToken } = requestBody; if (Number.isNaN(chainId)) {