Skip to content

Commit

Permalink
Merge pull request #42 from inove-jr/25-integração-com-captcha
Browse files Browse the repository at this point in the history
feat(recaptcha): implementação e validação do ReCAPTCHA v2 no formulá…
  • Loading branch information
paulohebert authored Jul 25, 2024
2 parents 40e7b1a + b3b3c45 commit 39f91c9
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 8 deletions.
8 changes: 8 additions & 0 deletions .http
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
### Obtem dados de planilha passando range e sheetName
GET http://localhost:3000/api/sheets?range=A:H&sheetName=projetos

### Integração com Recaptcha
POST http://localhost:3000/api/recaptcha
Content-Type: application/json

{
"token": "token"
}
157 changes: 149 additions & 8 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"@radix-ui/react-tooltip": "^1.1.2",
"@t3-oss/env-core": "^0.10.1",
"@t3-oss/env-nextjs": "^0.10.1",
"@types/react-google-recaptcha": "^2.1.9",
"axios": "^1.7.2",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"cmdk": "^1.0.0",
Expand All @@ -45,6 +47,7 @@
"react": "^18",
"react-day-picker": "^8.10.1",
"react-dom": "^18",
"react-google-recaptcha": "^3.1.0",
"react-hook-form": "^7.52.1",
"react-icons": "^5.2.1",
"tailwind-merge": "^2.3.0",
Expand Down
28 changes: 28 additions & 0 deletions src/app/api/recaptcha/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { NextRequest } from "next/server";
import axios from "axios";
import { z } from "zod";
import { env } from "@/env";

const schema = z.object({
token: z.string(),
});

export async function POST(req: NextRequest) {
try {
const { token } = schema.parse(await req.json());
const secretKey = env.GOOGLE_RECAPTCHA;

const response = await axios.post(
`https://www.google.com/recaptcha/api/siteverify?secret=${secretKey}&response=${token}`,
);

if (response.data.success)
return Response.json(response.data, { status: 200 });
else return Response.json(response.data, { status: 400 });
} catch (error) {
if (error instanceof z.ZodError) {
return Response.json(error.issues, { status: 400 });
}
return Response.json(error, { status: 500 });
}
}
Loading

0 comments on commit 39f91c9

Please sign in to comment.