Skip to content

Commit 648246c

Browse files
authored
Update login-pages.mdx
only enable submit button after Turnstile has completed
1 parent 11f2ca7 commit 648246c

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

src/content/docs/turnstile/tutorials/login-pages.mdx

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
title: Protect your login pages
2+
title: Protect your forms
33
pcx_content_type: tutorial
4-
updated: 2024-07-09
4+
updated: 2025-05-30
55
difficulty: Beginner
66
languages:
77
- JavaScript
@@ -12,12 +12,12 @@ sidebar:
1212

1313
---
1414

15-
This tutorial will guide you through integrating Cloudflare Turnstile to protect your login page. Learn how to implement the Turnstile widget on the client side and verify the Turnstile token via the siteverify API on the server side.
15+
This tutorial will guide you through integrating Cloudflare Turnstile to protect your web forms, such as login, signup, or contact forms. Learn how to implement the Turnstile widget on the client side and verify the Turnstile token via the siteverify API on the server side.
1616

1717
## Before you begin
1818

1919
- You must have a Cloudflare account.
20-
- You must have a web application with a login or signup page.
20+
- You must have a web application with a form you want to protect.
2121
- You must have basic knowledge of HTML and your server-side language of choice, such as Node.js or Python.
2222

2323
## Get Your Turnstile sitekey and secret key
@@ -28,26 +28,34 @@ This tutorial will guide you through integrating Cloudflare Turnstile to protect
2828

2929
## Add the Turnstile widget to your HTML form
3030

31-
1. Add the Turnstile widget to your login form.
31+
1. Add the Turnstile widget to your form.
3232
2. Replace `YOUR-SITE-KEY` with the sitekey from Cloudflare.
33+
3. Add a `data-callback` attribute to the Turnstile widget div. This JavaScript function will be called when the challenge is successful.
34+
4. Ensure your submit button is initially disabled.
3335

34-
```html title="Example" {13-14}
36+
```html title="Example" {14-15,17}
3537
<!DOCTYPE html>
3638
<html lang="en">
3739
<head>
3840
<meta charset="UTF-8">
39-
<title>Login Page</title>
41+
<title>Contact Form</title>
4042
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
43+
<script>
44+
function enableSubmit() {
45+
document.getElementById("submit-button").disabled = false;
46+
}
47+
</script>
4148
</head>
4249
<body>
43-
<form id="login-form" action="/login" method="POST">
44-
<input type="text" name="username" placeholder="Username" required>
45-
<input type="password" name="password" placeholder="Password" required>
50+
<form id="contact-form" action="/submit" method="POST">
51+
<input type="text" name="name" placeholder="Name" required>
52+
<input type="email" name="email" placeholder="Email" required>
53+
<textarea name="message" placeholder="Message" required></textarea>
4654

4755
<!-- Turnstile widget -->
48-
<div class="cf-turnstile" data-sitekey="YOUR-SITE-KEY"></div>
56+
<div class="cf-turnstile" data-sitekey="YOUR-SITE-KEY" data-callback="enableSubmit"></div>
4957

50-
<button type="submit">Log in</button>
58+
<button type="submit" id="submit-button" disabled>Submit</button>
5159
</form>
5260
</body>
5361
</html>
@@ -65,7 +73,7 @@ const app = express();
6573

6674
app.use(bodyParser.urlencoded({ extended: true }));
6775

68-
app.post('/login', async (req, res) => {
76+
app.post('/submit', async (req, res) => {
6977
const turnstileToken = req.body['cf-turnstile-response'];
7078
const secretKey = 'your-secret-key';
7179

@@ -78,11 +86,12 @@ app.post('/login', async (req, res) => {
7886
});
7987

8088
if (response.data.success) {
81-
// Token is valid, proceed with login
82-
const username = req.body.username;
83-
const password = req.body.password;
84-
// Your login logic here
85-
res.send('Login successful');
89+
// Token is valid, proceed with form submission
90+
const name = req.body.name;
91+
const email = req.body.email;
92+
const message = req.body.message;
93+
// Your form processing logic here
94+
res.send('Form submission successful');
8695
} else {
8796
res.status(400).send('Turnstile verification failed');
8897
}
@@ -100,13 +109,13 @@ app.listen(3000, () => {
100109

101110
It is crucial to handle the verification of the Turnstile token correctly. This section covers some key points to keep in mind.
102111

103-
### Verify the token after credentials input
112+
### Verify the token after form input
104113

105-
- Ensure that you verify the Turnstile token after the user has put in their credentials and selected **log in** to your website or application.
106-
- If you verify the token before the user inputs their credentials, a malicious visitor could bypass the protection by reentering the login credentials.
114+
- Ensure that you verify the Turnstile token after the user has filled out the form and selected **submit**.
115+
- If you verify the token before the user inputs their data, a malicious actor could potentially bypass the protection by manipulating the form submission after obtaining a valid token.
107116

108117
### Proper flow implementation
109118

110-
- When the user submits the login form, send both the login credentials and the Turnstile token to your server.
119+
- When the user submits the form, send both the form data and the Turnstile token to your server.
111120
- On the server side, verify the Turnstile token first.
112-
- Based on the verification response, decide whether to proceed with checking the login credentials.
121+
- Based on the verification response, decide whether to proceed with processing the form data.

0 commit comments

Comments
 (0)