Last active
January 11, 2016 13:53
-
-
Save oiami/997716600ed495d2a185 to your computer and use it in GitHub Desktop.
Example of Using Captcha::reCAPTCHA::V2
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
use Captcha::reCAPTCHA::V2; | |
any ['get', 'post'] => '/' => sub { | |
my $rc = Captcha::reCAPTCHA::V2->new; | |
my $widget = $rc->html('site_key', { theme => 'light' }); | |
template 'index' => { recaptcha => $widget }; | |
}; | |
post '/contact' => sub { | |
my $data = params; | |
my $rc = Captcha::reCAPTCHA::V2->new; | |
my $response = param( 'g-recaptcha-response' ); | |
my $result = $rc->verify('secret', $response); | |
if( $result->{is_valid} ){ | |
if( $data->{email} =~ /^([a-zA-Z][\w\_\.]{1,})\@([a-zA-Z0-9.-]+)\.([a-zA-Z]{2,})$/ ){ | |
email { | |
from => $data->{email}, | |
to => $contact_email, | |
subject => 'New contact form message', | |
body => $data->{message}, | |
}; | |
status '200'; | |
return to_json({ msg => 'Email is sent' }); | |
} else { | |
status '422'; | |
return to_json({ error => 'Email is invalid' }); | |
} | |
} else { | |
status '400'; | |
return to_json({ error => 'Human identification is invalid' }); | |
} | |
}; | |
true; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment