Skip to content

Commit 8bc9c1c

Browse files
authored
Added captcha (#415)
* Added captcha * Added captcha
1 parent 281814e commit 8bc9c1c

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

Diff for: components/auth/signup.tsx

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22
import Link from "next/link";
3-
import { useState } from "react";
3+
import { useState, useRef } from "react";
44
import { signinWithOAuth } from "@/app/(auth)/actions";
55
import { AdminUserAttributes, Provider } from "@supabase/supabase-js";
66
import { zodResolver } from "@hookform/resolvers/zod";
@@ -22,9 +22,12 @@ import { useRouter } from "next/navigation";
2222
import { Label } from "@/components/ui/label";
2323
import { Checkbox } from "@/components/ui/checkbox";
2424
import { useToggle } from "@/hooks/useToggle";
25+
import HCaptcha from "@hcaptcha/react-hcaptcha";
2526

2627
export default function SignUp() {
2728
const [isSubmitting, setIsSubmitting] = useState(false);
29+
const [captchaToken, setCaptchaToken] = useState<string | null>(null);
30+
const captcha = useRef<HCaptcha>(null);
2831
const form = useForm<SignUpFormData>({
2932
resolver: zodResolver(signUpSchema),
3033
defaultValues: {
@@ -42,14 +45,20 @@ export default function SignUp() {
4245
setIsSubmitting(true);
4346

4447
try {
45-
const formData: AdminUserAttributes = {
48+
if (!captchaToken) {
49+
toast.error("Please complete the captcha challenge");
50+
return;
51+
}
52+
53+
const formData = {
4654
email: data.email,
4755
password: data.password,
4856
user_metadata: {
4957
full_name: data.full_name,
5058
company_name: data.company_name,
5159
heard_about_us: data.heard_about_us,
5260
},
61+
captchaToken: captchaToken,
5362
};
5463
const response = await fetch("/api/signup", {
5564
method: "POST",
@@ -73,6 +82,8 @@ export default function SignUp() {
7382
toast.error("An unexpected error occurred. Please try again.");
7483
} finally {
7584
setIsSubmitting(false);
85+
captcha.current?.resetCaptcha();
86+
setCaptchaToken(null);
7687
}
7788
};
7889

@@ -252,6 +263,14 @@ export default function SignUp() {
252263
)}
253264
/>
254265

266+
<HCaptcha
267+
ref={captcha}
268+
sitekey="fa6c8c52-7694-45b0-97ec-7814072256b4"
269+
onVerify={(token) => {
270+
setCaptchaToken(token);
271+
}}
272+
/>
273+
255274
<div className="text-center text-sm text-gray-600">
256275
<Link
257276
href="/privacy"

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"prepare": "husky"
1212
},
1313
"dependencies": {
14+
"@hcaptcha/react-hcaptcha": "^1.11.2",
1415
"@headlessui/react": "^1.7.17",
1516
"@hookform/resolvers": "^3.6.0",
1617
"@radix-ui/react-accordion": "^1.2.0",

Diff for: yarn.lock

+20
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,13 @@
864864
dependencies:
865865
regenerator-runtime "^0.14.0"
866866

867+
"@babel/runtime@^7.17.9":
868+
version "7.26.9"
869+
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.26.9.tgz#aa4c6facc65b9cb3f87d75125ffd47781b475433"
870+
integrity sha512-aA63XwOkcl4xxQa3HjPMqOP6LiK0ZDv3mUPYEFXkpHbaFjtGggE1A61FjFzJnB+p7/oy2gA8E+rcBNl/zC1tMg==
871+
dependencies:
872+
regenerator-runtime "^0.14.0"
873+
867874
"@babel/template@^7.25.7":
868875
version "7.25.7"
869876
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.25.7.tgz#27f69ce382855d915b14ab0fe5fb4cbf88fa0769"
@@ -1215,6 +1222,19 @@
12151222
protobufjs "^7.2.5"
12161223
yargs "^17.7.2"
12171224

1225+
"@hcaptcha/loader@^1.2.1":
1226+
version "1.2.4"
1227+
resolved "https://registry.yarnpkg.com/@hcaptcha/loader/-/loader-1.2.4.tgz#541714395a82e27ec0f0e8bd80ef1a0bea141cc3"
1228+
integrity sha512-3MNrIy/nWBfyVVvMPBKdKrX7BeadgiimW0AL/a/8TohNtJqxoySKgTJEXOQvYwlHemQpUzFrIsK74ody7JiMYw==
1229+
1230+
"@hcaptcha/react-hcaptcha@^1.11.2":
1231+
version "1.11.2"
1232+
resolved "https://registry.yarnpkg.com/@hcaptcha/react-hcaptcha/-/react-hcaptcha-1.11.2.tgz#269556e312e579d0443084b00e3edc196a638757"
1233+
integrity sha512-F6+aZknrpTHoAhKIP80wWNJI5nhOJg0qyazM3pAw0bCyvVLSsveWeZyTK7W8EhO6nvovTE061gehlC+bmS/vMw==
1234+
dependencies:
1235+
"@babel/runtime" "^7.17.9"
1236+
"@hcaptcha/loader" "^1.2.1"
1237+
12181238
"@headlessui/react@^1.7.17":
12191239
version "1.7.19"
12201240
resolved "https://registry.yarnpkg.com/@headlessui/react/-/react-1.7.19.tgz#91c78cf5fcb254f4a0ebe96936d48421caf75f40"

0 commit comments

Comments
 (0)