Human Verification Modal for Anonymous Drupal Routes Using AJAX and Tokens
In a step-by-step tutorial, Andres Torres shows how to gate anonymous Drupal 10 routes behind a “verify you’re human” modal using Drupal’s AJAX API, expirable one-time tokens, and optional reCAPTCHA—offering a lightweight way to keep bots from hammering public endpoints.
The approach adds a custom access check that requires a single-use token, issues that token via an AJAX-loaded dialog form, and consumes it on success before the protected controller runs—no user accounts or heavy firewall rules required.
- Route + access check: Validates a
verify_tokenquery parameter via a custom service usingKeyValueExpirable. - AJAX modal form: Captures minimal data (e.g., email), optionally adds reCAPTCHA, and returns a one-time access link.
- Controller guard: Confirms and consumes the token, then executes the protected functionality.
- Theme-agnostic: Works with any theme; includes an optional JS behavior to close the dialog smoothly.
Ideal for public URLs where role-based access, throttling, or simple text CAPTCHAs aren’t enough, this pattern provides a clean UX while sharply reducing automated abuse.


