-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5aa40f1
commit feca96d
Showing
19 changed files
with
296 additions
and
2 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { NextRequest } from "next/server"; | ||
import { validateTurnstileToken } from "@courselit/utils"; | ||
|
||
export async function POST(req: NextRequest) { | ||
const body = await req.json(); | ||
const { token } = body; | ||
|
||
const verifyTurnstileToken = await validateTurnstileToken(token); | ||
if (verifyTurnstileToken) { | ||
return Response.json({ success: true }); | ||
} | ||
return Response.json({ success: false }, { status: 403 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { NextRequest } from "next/server"; | ||
|
||
export async function GET(req: NextRequest) { | ||
return Response.json( | ||
{ turnstileSiteKey: process.env.TURNSTILE_SITE_KEY }, | ||
{ status: 200 }, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface ServerConfig { | ||
turnstileSiteKey: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,4 +47,5 @@ export default { | |
frontend: "", | ||
}, | ||
typefaces: [], | ||
config: { turnstileSiteKey: "" }, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const SECRET_KEY = process.env.TURNSTILE_SECRET_KEY; | ||
|
||
export default async function validateTurnstileToken(token: string) { | ||
if (!SECRET_KEY) { | ||
throw new Error("Turnstile: No secret key found"); | ||
} | ||
|
||
const formData = new FormData(); | ||
formData.append("secret", SECRET_KEY || ""); | ||
formData.append("response", token || ""); | ||
|
||
const url = "https://challenges.cloudflare.com/turnstile/v0/siteverify"; | ||
const result = await fetch(url, { | ||
body: formData, | ||
method: "POST", | ||
}); | ||
|
||
const outcome = await result.json(); | ||
if (outcome.success) { | ||
return true; | ||
} | ||
if ( | ||
outcome["error-codes"] && | ||
outcome["error-codes"][0] === "timeout-or-duplicate" | ||
) { | ||
return true; | ||
} | ||
// eslint-disable-next-line no-console | ||
console.log(`Turnstile validation failed`, outcome); | ||
return false; | ||
} |
Oops, something went wrong.