Skip to content

Commit

Permalink
added captcha to login and register page
Browse files Browse the repository at this point in the history
  • Loading branch information
VijeshVS committed Jun 2, 2024
1 parent f0a6322 commit 660009f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 29 deletions.
50 changes: 23 additions & 27 deletions app/app/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ import { useState } from "react";
import { useToast } from "@/components/ui/use-toast";
import { useRouter } from "next/navigation";
import { SkeletonCard } from "@/components/CardComponents/SkeletonCard";
import { KeyRound } from "lucide-react";
import { Turnstile } from '@marsidev/react-turnstile'
import { captchaVerify } from "@/lib/actions/captchaVerify";

const LoginPage = () => {
const [email, setEmail] = useState("");
const [pass, setPass] = useState("");
const { toast } = useToast();
const router = useRouter();
const [loading, setLoading] = useState(true);
const [token,setToken] = useState<string>("")

const { data } = useSession();
useEffect(() => {
Expand All @@ -43,21 +45,6 @@ const LoginPage = () => {
</div>
);

const handleGoogleLogin = async () => {
const res: any = await signIn("google");
if (res && res.status == 200) {
toast({
title: "User logged in successfully !!",
});
router.push("/app/home");
} else {
toast({
title: "Error while logging in!!",
variant: "destructive",
});
}
}

return (
<div className="flex w-full h-screen justify-center items-center px-4">
<Card className="w-[400px]">
Expand Down Expand Up @@ -85,13 +72,30 @@ const LoginPage = () => {
/>
</CardContent>
<CardFooter>
<div className="flex flex-col items-center w-full">
<Button className="w-32"
<div className="flex flex-col items-center w-full">

<Turnstile onSuccess={(token) => {
setToken(token)
}} siteKey='0x4AAAAAAAbu8T-y1tAsroOZ' />

This comment has been minimized.

Copy link
@vigneshshettyin

vigneshshettyin Jun 2, 2024

Owner

Refer 1


<Button disabled={token==""} className="w-32 mt-4"
onClick={async () => {
const verify = await captchaVerify(token);

if(verify == 403){
toast({
title:"Captcha invalid",
description: "Please try again",
variant:"destructive"
})
return;
}

const res: any = await signIn("credentials", {
email: email,
password: pass,
redirect: false,
token:token
});
if (res.status == 200) {
toast({
Expand All @@ -107,16 +111,8 @@ const LoginPage = () => {
}}
>
Login
</Button>
{/* <div className="flex justify-center">
<Button className="mt-4" onClick={handleGoogleLogin}>
<KeyRound size={18}/>
<h1 className="ml-2">Sign in with google</h1>
</Button>
</div> */}

</Button>
</div>

</CardFooter>
</Card>
</div>
Expand Down
22 changes: 20 additions & 2 deletions app/app/register/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,29 @@ import { toast } from "@/components/ui/use-toast";
import { redirect } from "next/navigation";
import { useSession } from "next-auth/react";
import { SkeletonCard } from "@/components/CardComponents/SkeletonCard";
import { Turnstile } from "@marsidev/react-turnstile";
import { captchaVerify } from "@/lib/actions/captchaVerify";



const RegisterPage = () => {
const [loading, setLoading] = useState(true);
const [token,setToken] = useState<string>("")

const registerUser = async (formData: FormData) => {
const verify = await captchaVerify(token);

if(verify == 403){
toast({
title:"Invalid Captcha",
description: "Please try again",
variant:"destructive"
})
return;
}

const status = await register(formData);

if (status == 200) {
toast({
title: "User registered successfully !!",
Expand Down Expand Up @@ -86,8 +101,11 @@ const RegisterPage = () => {
/>
</CardContent>
<CardFooter>
<div className="flex justify-center w-full">
<Button>Register</Button>
<div className="flex flex-col items-center w-full">
<Turnstile onSuccess={(token) => {
setToken(token)
}} siteKey='0x4AAAAAAAbu8T-y1tAsroOZ' />

This comment has been minimized.

Copy link
@vigneshshettyin

vigneshshettyin Jun 2, 2024

Owner
  1. Move site_id to env
<Button className="mt-4" disabled={token == ""}>Register</Button>
</div>
</CardFooter>
</form>
Expand Down
25 changes: 25 additions & 0 deletions lib/actions/captchaVerify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use server'
import { HTTP_STATUS } from "../constants";

export async function captchaVerify (token:string) {
const SECRET_KEY = "0x4AAAAAAAbu8XnHHmSbg9kLI3T3HziPSuI"

This comment has been minimized.

Copy link
@vigneshshettyin

vigneshshettyin Jun 2, 2024

Owner

Avoid using secrets directly, use env variables


let formData = new FormData();
formData.append('secret', SECRET_KEY);
formData.append('response', token);

const url = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';

This comment has been minimized.

Copy link
@vigneshshettyin

vigneshshettyin Jun 2, 2024

Owner

Move this to env

const result = await fetch(url, {
body: formData,
method: 'POST',
});

const challengeSucceeded = (await result.json()).success;

if (!challengeSucceeded) {
return HTTP_STATUS.FORBIDDEN
}
else{
return HTTP_STATUS.OK
}
}
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"postinstall": "prisma generate"
},
"dependencies": {
"@marsidev/react-turnstile": "^0.6.1",
"@prisma/client": "^5.13.0",
"@radix-ui/react-accordion": "^1.1.2",
"@radix-ui/react-aspect-ratio": "^1.0.3",
Expand Down

0 comments on commit 660009f

Please sign in to comment.