1+ import { COOKIE_ACTIVE_ACCOUNT , COOKIE_PREFIX_TOKEN } from "@/constants/cookie" ;
2+ import { API_SERVER_URL } from "@/constants/env" ;
13import { startOfToday } from "date-fns" ;
24import { cacheGet , cacheSet } from "lib/redis" ;
35import { type NextRequest , NextResponse } from "next/server" ;
4- import { ZERO_ADDRESS } from "thirdweb" ;
6+ import { ZERO_ADDRESS , getAddress } from "thirdweb" ;
57import { getFaucetClaimAmount } from "./claim-amount" ;
68
79const 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.
2123export 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