From 638445a6043ad85c0c3c34943e59e6a1547ed5c7 Mon Sep 17 00:00:00 2001 From: primata Date: Mon, 5 Aug 2024 14:30:32 -0300 Subject: [PATCH] revert to pre recaptcha enterprise --- .gitignore | 2 + api/rate-limit.ts | 32 +++++++--- src/components/Chain.tsx | 129 ++++++++++++++++++--------------------- 3 files changed, 87 insertions(+), 76 deletions(-) diff --git a/.gitignore b/.gitignore index ff4917ca..89c5deb9 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,5 @@ movement-explorer.txt yarn.lock .vercel + +GOOGLE_APPLICATION_CREDENTIALS.json diff --git a/api/rate-limit.ts b/api/rate-limit.ts index d778f749..9d635f06 100644 --- a/api/rate-limit.ts +++ b/api/rate-limit.ts @@ -91,14 +91,30 @@ export default async function handler(request: any, response: any) { const { success, pending, limit, reset, remaining } = await ratelimit.limit( ip[0] ); - console.log('ratelimit', success, pending, limit, reset, remaining) - const score = await createAssessment("movement-faucet-1722352143785", "6LdVjR0qAAAAAFSjzYqyRFsnUDn-iRrzQmv0nnp3", "", token); - console.log('score', score) - if (score === (null || undefined)) { - response.status(400).json({ error: 'Invalid reCAPTCHA token' }); - } else if (score != null && score < 0.5) { - response.status(400).json({ error: 'reCAPTCHA score too low' }); - } else { + + // const score = await createAssessment("movement-faucet-1722352143785", "6LdVjR0qAAAAAFSjzYqyRFsnUDn-iRrzQmv0nnp3", "", token); + // console.log('score', score) + // if (score === (null || undefined)) { + // response.status(400).json({ error: 'Invalid reCAPTCHA token' }); + // } else if (score != null && score < 0.5) { + // response.status(400).json({ error: 'reCAPTCHA score too low' }); + // } else { + // response.status(success ? 200 : 429).json({ success, pending, limit, reset, remaining }); + // } + + try { + const verification = await fetch(verificationUrl, { + method: 'POST', + }); + const data = await verification.json(); + console.log('data', data) + if (data.success == false) { + response.status(400).json({ error: 'Invalid reCAPTCHA token' }); + } + console.log('verification successful', data.success) + console.log('not rate limited', success) response.status(success ? 200 : 429).json({ success, pending, limit, reset, remaining }); + } catch (error) { + response.status(500).json({ error: 'Server error' }); } } \ No newline at end of file diff --git a/src/components/Chain.tsx b/src/components/Chain.tsx index 96e5aaa5..950ca5f5 100644 --- a/src/components/Chain.tsx +++ b/src/components/Chain.tsx @@ -1,23 +1,23 @@ import React, { useEffect, useRef } from "react"; import Box from "@mui/material/Box"; import Button from "@mui/material/Button"; -import { useState,RefObject } from "react"; +import { useState, RefObject } from "react"; import TextField from "@mui/material/TextField"; import Container from "@mui/material/Container"; import Alert from "@mui/material/Alert"; import CircularProgress from "@mui/material/CircularProgress"; -import { Switch,useTheme } from "@mui/material"; +import { Switch, useTheme } from "@mui/material"; import ReCAPTCHA from "react-google-recaptcha"; -export default function Chains({ name,eventName, language, amount, isEvm, network, faucetRequest }: any) { +export default function Chains({ name, eventName, language, amount, isEvm, network, faucetRequest }: any) { const [address, setAddress] = useState(""); const [success, setSuccess] = useState(false); const [errorMessage, setErrorMessage] = useState(null); const [loading, setLoading] = useState(false); const recaptchaRef = useRef(null); - const [token, setToken] = useState(null); + const [token, setToken] = useState(null); const theme = useTheme(); const [isDark, setIsDark] = useState(theme.palette.mode === "dark"); useEffect(() => { @@ -39,82 +39,75 @@ export default function Chains({ name,eventName, language, amount, isEvm, networ useEffect(() => { if (recaptchaRef.current) { - // If you need to do anything with the recaptchaRef, you can do it here - console.log('ReCAPTCHA is ready'); + // If you need to do anything with the recaptchaRef, you can do it here + console.log('ReCAPTCHA is ready'); } - }, []); + }, []); - const onChangeRe = (value:string|null)=> { + const onChangeRe = (value: string | null) => { // console.log("Captcha value:", value); setToken(value); - } - - const checkRateLimit = async (token:string) => { + } + + const checkRateLimit = async (token: string) => { try { - const response = await fetch('/api/rate-limit', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ token }), + const response = await fetch('/api/rate-limit', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ token }), }); - // Check if the response is an HTML page - const contentType = response.headers.get('content-type'); - if (!contentType || !contentType.includes('application/json')) { - const text = await response.text(); - throw new Error(`Expected JSON but received: ${text}`); - } - - const rateLimitData = await response.json(); - console.log(rateLimitData) - if (rateLimitData) return !rateLimitData.success; - - if (!response.ok) { - // Handle rate limit exceeded - setErrorMessage('Rate limit exceeded. Please try again later.'); - return true; - } else { - // Handle successful rate limit check - return false; - } + // Check if the response is an HTML page + const contentType = response.headers.get('content-type'); + if (!contentType || !contentType.includes('application/json')) { + const text = await response.text(); + throw new Error(`Expected JSON but received: ${text}`); + } + + const rateLimitData = await response.json(); + console.log('Limit:', rateLimitData.limit); + if (response.status == 200) { + return false; + } else { + return true; + } } catch (error) { - setErrorMessage('Error fetching rate limit data'); - return true; + console.error('Error checking rate limit:', error); + return true; } }; - + const handleRequest = async () => { setLoading(true); - + if (recaptchaRef.current === null) return console.log("recaptchaRef is null"); const captchaValue = recaptchaRef?.current.getValue() if (!captchaValue) { setErrorMessage("Please complete the captcha."); - setLoading(false); - return; - } - const rateLimited = await checkRateLimit(captchaValue); - if (rateLimited) { - setErrorMessage('Rate limit exceeded. Please try again later.'); - setLoading(false); - return; - } - - let status = false; - const res = await faucetRequest(address, token); - console.log(res) - if (res.error) { - setErrorMessage(res.error || "Failed to fund account."); - } else if (res) { - { - setSuccess(true); - status = true; - } + } else { + const rateLimited = await checkRateLimit(captchaValue); + if (rateLimited) { + setErrorMessage('Rate limit exceeded. Please try again later.'); + } else { + let status = false; + const res = await faucetRequest(address, token); + console.log(res) + if (res.error) { + setErrorMessage(res.error || "Failed to fund account."); + } else if (res) { + { + setSuccess(true); + status = true; + } + } + } } recaptchaRef.current?.reset(); setToken(null); setLoading(false); + }; const handleFormSubmit = (event: React.FormEvent) => { @@ -182,14 +175,14 @@ export default function Chains({ name,eventName, language, amount, isEvm, networ
- +
{success && Funded account {_amount} MOVE} {errorMessage && {errorMessage}}