Skip to content

Commit 656c1f5

Browse files
authored
Sign in captcha auth (#418)
* Progress * Progress * Added public key * Center align
1 parent 8bc9c1c commit 656c1f5

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

Diff for: app/(auth)/actions.ts

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export async function signin(
2121
const data: SignInWithPasswordCredentials = {
2222
email: formData.get("email") as string,
2323
password: formData.get("password") as string,
24+
options: { captchaToken: formData.get("captchaToken") as string },
2425
};
2526

2627
const { data: res, error } = await supabase.auth.signInWithPassword(data);

Diff for: components/auth/signin.tsx

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"use client";
22
import Link from "next/link";
3-
import { useState } from "react";
3+
import { useState, useRef } from "react";
4+
import HCaptcha from "@hcaptcha/react-hcaptcha";
45
import { zodResolver } from "@hookform/resolvers/zod";
56
import { useForm } from "react-hook-form";
67
import { signin, signinWithOAuth } from "@/app/(auth)/actions";
8+
import { HCAPTCHA_SITE_KEY_PUBLIC } from "@/utils/constants";
79
import { Provider } from "@supabase/supabase-js";
810
import { Button } from "@/components/ui/button";
911
import { Input } from "@/components/ui/input";
@@ -25,6 +27,8 @@ import { useToggle } from "@/hooks/useToggle";
2527
export default function SignIn() {
2628
const [isSubmitting, setIsSubmitting] = useState(false);
2729
const [errorMessage, setErrorMessage] = useState<string | null>(null);
30+
const [captchaToken, setCaptchaToken] = useState<string>();
31+
const captcha = useRef<HCaptcha>(null);
2832
const searchParams = useSearchParams();
2933
const callbackForDesktopApp = searchParams?.get("callback") ?? "";
3034

@@ -42,9 +46,15 @@ export default function SignIn() {
4246
setErrorMessage(null);
4347

4448
try {
49+
if (!captchaToken) {
50+
setErrorMessage("Please complete the CAPTCHA");
51+
return;
52+
}
53+
4554
const formData = new FormData();
4655
formData.append("email", data.email);
4756
formData.append("password", data.password);
57+
formData.append("captchaToken", captchaToken);
4858

4959
const response = await signin(formData, callbackForDesktopApp);
5060
if (response?.error) {
@@ -56,6 +66,8 @@ export default function SignIn() {
5666
setErrorMessage("An unexpected error occurred. Please try again.");
5767
} finally {
5868
setIsSubmitting(false);
69+
captcha.current?.resetCaptcha();
70+
setCaptchaToken(undefined);
5971
}
6072
};
6173

@@ -193,6 +205,17 @@ export default function SignIn() {
193205
Forgot Password?
194206
</Link>
195207
</div>
208+
209+
<div className="flex justify-center">
210+
<HCaptcha
211+
ref={captcha}
212+
sitekey={HCAPTCHA_SITE_KEY_PUBLIC}
213+
onVerify={(token) => {
214+
setCaptchaToken(token);
215+
}}
216+
/>
217+
</div>
218+
196219
<Button
197220
type="submit"
198221
size="lg"

Diff for: components/auth/signup.tsx

+10-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import Link from "next/link";
33
import { useState, useRef } from "react";
44
import { signinWithOAuth } from "@/app/(auth)/actions";
5+
import { HCAPTCHA_SITE_KEY_PUBLIC } from "@/utils/constants";
56
import { AdminUserAttributes, Provider } from "@supabase/supabase-js";
67
import { zodResolver } from "@hookform/resolvers/zod";
78
import { useForm } from "react-hook-form";
@@ -263,13 +264,15 @@ export default function SignUp() {
263264
)}
264265
/>
265266

266-
<HCaptcha
267-
ref={captcha}
268-
sitekey="fa6c8c52-7694-45b0-97ec-7814072256b4"
269-
onVerify={(token) => {
270-
setCaptchaToken(token);
271-
}}
272-
/>
267+
<div className="flex justify-center">
268+
<HCaptcha
269+
ref={captcha}
270+
sitekey={HCAPTCHA_SITE_KEY_PUBLIC}
271+
onVerify={(token) => {
272+
setCaptchaToken(token);
273+
}}
274+
/>
275+
</div>
273276

274277
<div className="text-center text-sm text-gray-600">
275278
<Link

Diff for: utils/constants.ts

+2
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ export const footerSections = [
211211
},
212212
];
213213

214+
export const HCAPTCHA_SITE_KEY_PUBLIC = "fa6c8c52-7694-45b0-97ec-7814072256b4";
215+
214216
export const socialMediaLinks = [
215217
{
216218
icon: GitHubLogo,

0 commit comments

Comments
 (0)