Coder Social home page Coder Social logo

laravel-recaptcha's Introduction

Laravel ReCAPTCHA

This is a Laravel package for Google reCAPTCHA v3.

Installation

You can install the package via composer:

composer require goedemiddag/laravel-recaptcha

Add the following environment variables to your .env file:

RECAPTCHA_SITE_KEY=YOUR_SITE_KEY
RECAPTCHA_SECRET_KEY=YOUR_SECRET_KEY
RECAPTCHA_THRESHOLD=0.3

Config

You can optionally publish the config file:

php artisan vendor:publish --provider="Goedemiddag\ReCaptcha\ReCaptchaServiceProvider" --tag="recaptcha-config"

This is the contents of the published config file:

return [
    'site_key' => env('RECAPTCHA_SITE_KEY'),
    'secret_key' => env('RECAPTCHA_SECRET_KEY'),

    // The threshold to pass the recaptcha validation, from 0 (easiest) to 1 (hardest)
    'threshold' => env('RECAPTCHA_THRESHOLD', 0.3),

    // Provide IP addresses that shouldn't be validated
    'skip_ips' => [
        // 127.0.0.1
    ],
];

Translations

This package offers Dutch and English translations by default. You can optionally publish the translations:

php artisan vendor:publish --provider="Goedemiddag\ReCaptcha\ReCaptchaServiceProvider" --tag="recaptcha-lang"

Usage

This package aims to provide the Google ReCaptcha v3 validation in the backend of your application and offers some tools to use it in the frontend.

Quick start

Add the <x-recaptcha::input /> blade component to your form:

<form method="POST">
    @csrf
    <x-recaptcha::input />
    
    ... 
    
    <button type="submit">Submit</button>
</form>

Add the <x-recaptcha::script /> blade component to your end of your layout:

<body>
    ...page content...

    <x-recaptcha::script />
</body>

Bind an event listener to the form submit event, please adjust this for your application:

document.querySelector('form').addEventListener('submit', function (event) {
    event.preventDefault();
    
    window.reCaptcha.render('submit', function (token) {        
        // Set token value
        event.target.querySelector('input[name="g-recaptcha-response"]').value = token;
        
        // Submit form
        event.target.submit();        
    });
});

Add the ReCaptchaRule to your form request:

public function rules(): array
{
    return [
        'g-recaptcha-response' => ['required', new ReCaptchaRule()],
    ];
}

Contributing

Found a bug or want to add a new feature? Great! There are also many other ways to make meaningful contributions such as reviewing outstanding pull requests and writing documentation. Even opening an issue for a bug you found is appreciated.

When you create a pull request, make sure it is tested, following the code standard (run composer pint:fix to take care of that for you) and please create one pull request per feature. In exchange, you will be credited as contributor.

laravel-recaptcha's People

Contributors

dvdheiden avatar dcmastenbroek avatar bonroyage avatar

Stargazers

 avatar  avatar

Watchers

Johan Krijt avatar  avatar  avatar

laravel-recaptcha's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.