AngularJS directive to add a Google reCaptcha to your form

recaptcha-3

Google reCaptcha is very popular captcha widget from google which you can add in your web form to prevent filling span data in your form or auto generated data. It easily check is real human filling form or some program entering data in form and tying to submit. So in this post I am going to share this popular captcha widget for your angular based app. which you can easily apply on your angular form.

recaptcha-3

Bower

bower install --save angular-recaptcha

npm

npm install --save angular-recaptcha

Usage

IMPORTANT: Keep in mind that the captcha only works when used from a real domain
and with a valid re-captcha key, so this file won't work if you just load it in
your browser.

This the recommended way of using the reCaptcha directive.
Instead of using ng-model to get the challenge and response, you use a service.
This way it's safer because we don't depend on a timeout hack that we are currently using in the directive.

Other Parameters

You can optionally pass a theme the captcha should use, as an HTML attribute:

    

Language Codes: https://developers.google.com/recaptcha/docs/language

In this case we are specifying that the captcha should use the theme named light.

Listeners

There are three listeners you can use with the directive, on-create, on-success, and on-expire.

  • on-create: It’s called right after the widget is created. It receives a widget ID, which could be helpful if you have more than one reCaptcha in your site.
  • on-success: It’s called once the user resolves the captcha. It receives the response string you would need for verifying the response.
  • on-expire: It’s called when the captcha response expires and the user needs to solve a new captcha.

Example

app.controller('myController', ['$scope', 'vcRecaptchaService', function ($scope, recaptcha) {
    $scope.setWidgetId = function (widgetId) {
        // store the `widgetId` for future usage.
        // For example for getting the response with
        // `recaptcha.getResponse(widgetId)`.
    };

    $scope.setResponse = function (response) {
        // send the `response` to your server for verification.
    };

    $scope.cbExpiration = function() {
        // reset the 'response' object that is on scope
    };
}]);

Secure Token

If you want to use a secure token pass it along with the site key as an HTML attribute.


Please note that you have to encrypt your token yourself with your private key upfront!
To learn more about secure tokens and how to generate & encrypt them please refer to the reCAPTCHA Docs.

Service Provider

You can use the vcRecaptchaServiceProvider to configure the recaptcha service once in your application’s config function.
This is a convenient way to set your reCaptcha site key, theme, stoken, size, and type in one place instead of each vc-recaptcha directive element instance.
The defaults defined in the service provider will be overrode by any values passed to the vc-recaptcha directive element for that instance.

myApp.config(function(vcRecaptchaServiceProvider){
  vcRecaptchaServiceProvider.setSiteKey('---- YOUR PUBLIC KEY GOES HERE ----')
  vcRecaptchaServiceProvider.setTheme('---- light or dark ----')
  vcRecaptchaServiceProvider.setStoken('--- YOUR GENERATED SECURE TOKEN ---')
  vcRecaptchaServiceProvider.setSize('---- compact, normal or invisible ----')
  vcRecaptchaServiceProvider.setType('---- audio or image ----')
  vcRecaptchaServiceProvider.setLang('---- language code ----')
});

Language Codes: https://developers.google.com/recaptcha/docs/language

You can also set all of the values at once.

myApp.config(function(vcRecaptchaServiceProvider){
  vcRecaptchaServiceProvider.setDefaults({
    key: '---- YOUR PUBLIC KEY GOES HERE ----',
    theme: '---- light or dark ----',
    stoken: '--- YOUR GENERATED SECURE TOKEN ---',
    size: '---- compact, normal or invisible ----',
    type: '---- audio or image ----',
    lang: '---- language code ----'
  });
});

See live demo and download source code.

This awesome script developed by VividCortex, Visit their official github repository for more information and follow for future updates.


Don’t forget to Subscribe My Public Notebook for more useful free scripts, tutorials and articles.

Leave a Reply

Your email address will not be published. Required fields are marked *

Top