Skip to content

Commit

Permalink
Sign in captcha auth (#418)
Browse files Browse the repository at this point in the history
* Progress

* Progress

* Added public key

* Center align
  • Loading branch information
nang-dev authored Feb 17, 2025
1 parent 8bc9c1c commit 656c1f5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/(auth)/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export async function signin(
const data: SignInWithPasswordCredentials = {
email: formData.get("email") as string,
password: formData.get("password") as string,
options: { captchaToken: formData.get("captchaToken") as string },
};

const { data: res, error } = await supabase.auth.signInWithPassword(data);
Expand Down
25 changes: 24 additions & 1 deletion components/auth/signin.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"use client";
import Link from "next/link";
import { useState } from "react";
import { useState, useRef } from "react";
import HCaptcha from "@hcaptcha/react-hcaptcha";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { signin, signinWithOAuth } from "@/app/(auth)/actions";
import { HCAPTCHA_SITE_KEY_PUBLIC } from "@/utils/constants";
import { Provider } from "@supabase/supabase-js";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
Expand All @@ -25,6 +27,8 @@ import { useToggle } from "@/hooks/useToggle";
export default function SignIn() {
const [isSubmitting, setIsSubmitting] = useState(false);
const [errorMessage, setErrorMessage] = useState<string | null>(null);
const [captchaToken, setCaptchaToken] = useState<string>();
const captcha = useRef<HCaptcha>(null);
const searchParams = useSearchParams();
const callbackForDesktopApp = searchParams?.get("callback") ?? "";

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

try {
if (!captchaToken) {
setErrorMessage("Please complete the CAPTCHA");
return;
}

const formData = new FormData();
formData.append("email", data.email);
formData.append("password", data.password);
formData.append("captchaToken", captchaToken);

const response = await signin(formData, callbackForDesktopApp);
if (response?.error) {
Expand All @@ -56,6 +66,8 @@ export default function SignIn() {
setErrorMessage("An unexpected error occurred. Please try again.");
} finally {
setIsSubmitting(false);
captcha.current?.resetCaptcha();
setCaptchaToken(undefined);
}
};

Expand Down Expand Up @@ -193,6 +205,17 @@ export default function SignIn() {
Forgot Password?
</Link>
</div>

<div className="flex justify-center">
<HCaptcha
ref={captcha}
sitekey={HCAPTCHA_SITE_KEY_PUBLIC}
onVerify={(token) => {
setCaptchaToken(token);
}}
/>
</div>

<Button
type="submit"
size="lg"
Expand Down
17 changes: 10 additions & 7 deletions components/auth/signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import Link from "next/link";
import { useState, useRef } from "react";
import { signinWithOAuth } from "@/app/(auth)/actions";
import { HCAPTCHA_SITE_KEY_PUBLIC } from "@/utils/constants";
import { AdminUserAttributes, Provider } from "@supabase/supabase-js";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
Expand Down Expand Up @@ -263,13 +264,15 @@ export default function SignUp() {
)}
/>

<HCaptcha
ref={captcha}
sitekey="fa6c8c52-7694-45b0-97ec-7814072256b4"
onVerify={(token) => {
setCaptchaToken(token);
}}
/>
<div className="flex justify-center">
<HCaptcha
ref={captcha}
sitekey={HCAPTCHA_SITE_KEY_PUBLIC}
onVerify={(token) => {
setCaptchaToken(token);
}}
/>
</div>

<div className="text-center text-sm text-gray-600">
<Link
Expand Down
2 changes: 2 additions & 0 deletions utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ export const footerSections = [
},
];

export const HCAPTCHA_SITE_KEY_PUBLIC = "fa6c8c52-7694-45b0-97ec-7814072256b4";

export const socialMediaLinks = [
{
icon: GitHubLogo,
Expand Down

0 comments on commit 656c1f5

Please sign in to comment.