Skip to content

Commit

Permalink
revert to pre recaptcha enterprise
Browse files Browse the repository at this point in the history
  • Loading branch information
0xPrimata committed Aug 5, 2024
1 parent 49ce784 commit 638445a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 76 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ movement-explorer.txt

yarn.lock
.vercel

GOOGLE_APPLICATION_CREDENTIALS.json
32 changes: 24 additions & 8 deletions api/rate-limit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
}
}
129 changes: 61 additions & 68 deletions src/components/Chain.tsx
Original file line number Diff line number Diff line change
@@ -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<string | null>(null);
const [loading, setLoading] = useState(false);
const recaptchaRef = useRef<ReCAPTCHA>(null);
const [token, setToken] = useState<string|null>(null);
const [token, setToken] = useState<string | null>(null);
const theme = useTheme();
const [isDark, setIsDark] = useState(theme.palette.mode === "dark");
useEffect(() => {
Expand All @@ -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) => {
Expand Down Expand Up @@ -182,14 +175,14 @@ export default function Chains({ name,eventName, language, amount, isEvm, networ
</Button>
<div>

<ReCAPTCHA
ref={recaptchaRef}
sitekey={language == 'aptos' ? process.env.REACT_APP_RECAPTCHA_APTOS_PUBLIC_KEY??'' : "6LdPgxMqAAAAAByFdD5V8PiPKYZS4mSZWUUcZW6B"}
// size="invisible"
hl="en"
onChange={onChangeRe}
theme="dark"
/>
<ReCAPTCHA
ref={recaptchaRef}
sitekey={language == 'aptos' ? process.env.REACT_APP_RECAPTCHA_APTOS_PUBLIC_KEY ?? '' : "6LdPgxMqAAAAAByFdD5V8PiPKYZS4mSZWUUcZW6B"}
// size="invisible"
hl="en"
onChange={onChangeRe}
theme="dark"
/>
</div>
{success && <Alert severity="success" sx={{ width: 300, marginBottom: 2 }}>Funded account {_amount} MOVE</Alert>}
{errorMessage && <Alert severity="error" sx={{ width: 300, marginBottom: 2 }}>{errorMessage}</Alert>}
Expand Down

0 comments on commit 638445a

Please sign in to comment.