Implement Google's reCaptcha service in Vibe.d.
You'll need to sign up for API Keys before you can use reCaptcha
To install, add vibe_recaptcha to your package.json file:
"dependencies": { "vibe-d":">=0.7.18", "vibe_recaptcha":">=1.0" }
Import the module in your app.d file:
import recaptcha;
The CAPTCHA image is generated in two steps
1. Call function recaptchaHTML(string publickey) in your app.d file, and make it visible to your view:
void index(HTTPServerRequest req, HTTPServerResponse res) { string recaptcha = recaptchaHTML("your public key"); res.renderCompat!("index.dt", HTTPServerRequest, "req", string, "recaptcha") (req, recaptcha); }
2. From there, render the variable from within your view:
form(action='/verify',method='post') // Your form fields here != recaptcha input(type='submit')
Check the response with verifyRecaptcha(string privateKey, string ip, string challenge, string response)
void verify(HTTPServerRequest req, HTTPServerResponse res) { string challenge = req.form["recaptcha_challenge_field"]; string response = req.form["recaptcha_response_field"]; if ( verifyRecaptcha("private key", req.peer, challenge, response) ) // Handle the success response res.redirect("/success"); else // Handle the failure response res.writeBody(cast(ubyte[]) "Failure.", "text/plain"); }
To check for errors during verification, call function testRecaptcha instead of verifyRecaptcha to see the response from reCAPTCHA when a response is inputted:
// import vibe.core.log void verify(HTTPServerRequest req, HTTPServerResponse res) { string challenge = req.form["recaptcha_challenge_field"]; string response = req.form["recaptcha_response_field"];// Returns either "true" or an error message string recaptchaMessage = testRecaptcha("private key", req.peer, challenge, response); logInfo("Recaptcha's Response: %s", recaptchaMessage);
}