GithubHelp home page GithubHelp logo

rajeshdewle / google-recaptcha Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chitranu/google-recaptcha

0.0 0.0 0.0 11 KB

Easily validate recaptcha responses in Laravel 7. A handy validation rule for validating the reCAPTCHA token in the form requests.

License: MIT License

PHP 100.00%

google-recaptcha's Introduction

Google reCAPTCHA validater for Laravel

Latest Version on Packagist Total Downloads

This package is a wrapper around Google's reCAPTCHA PHP client library. It provides a handy validation rule recaptcha, which can be used to validate the reCAPTCHA token in the form requests.

You can use this package with any of reCAPTCHA types:

  • Google reCAPTCHA v2
  • Google reCAPTCHA v3

Installation

You can install the package via composer:

composer require chitranu/google-recaptcha

Usage

Get Google reCAPTCHA secret key for your application from https://www.google.com/recaptcha/admin/ and place it inside .env file at the root like this.

GOOGLE_RECAPTCHA_SECRETKEY=YOUR_RECAPTCHA_SECRET_KEY

After setting secret key, head over to your request validator, and add a field with rule the recaptcha like below to validate the token received in the form request.

$request->validate([
    '...' // other fields
    'recaptcha-token' => 'required|recaptcha'
]);

Using in frontend (vue-recaptcha-v3 plugin)

This package is intended to use with vue-recaptcha-v3 npm plugin. You can use it by creating a Vue form component using vue-recaptcha-v3 plugin shown below.

Register your site key with the vue-recaptcha-v3 plugin:

import Vue from 'vue'
import { VueReCaptcha } from 'vue-recaptcha-v3'

Vue.use(VueReCaptcha, { siteKey: 'YOUR_GOOGLE_SITE_KEY' })

Create a Vue component for the form and submit reCAPTCHA token using form like this:

<template>
	<form @submit.prevent="onFormSubmit()" ref="contactform">
        <input type="text" name="name" placeholder="Your Name"/>
        <input type="email" name="email" placeholder="Your Email"/>
        <textarea name="message" placeholder="Your Message"></textarea>
	 	<button  type="submit">Submit</button>
	</form>
</template>

<script>
export default {
  methods: {
    onFormSubmit() {
    // Wait until recaptcha has been loaded.
    await this.$recaptchaLoaded()

    // Execute reCAPTCHA with action "login".
    const token = await this.$recaptcha('login')

    // Prepare form data
    let formData = new FormData(this.$refs.contactform);

    // Appended token in formData
    formData.append('recaptcha-token', token);

    // Make an ajax request to your Laravel endpoint.
    axios.post('/your-form-endpoint', formData)
      .then(response => {
        // handle response
      }, error => {
        // handle errors
      })
    }
  }
}
</script>

Using in frontend (vue-recaptcha plugin)

If you are using vue-recaptcha plugin (older version), you can still use it by creating a vue form component shown below:

<template>
	<form @submit.prevent="onFormSubmit()" ref="contactform">
        <input type="text" name="name" placeholder="Your Name"/>
        <input type="email" name="email" placeholder="Your Email"/>
        <textarea name="message" placeholder="Your Message"></textarea>
        <vue-recaptcha
            ref="recaptcha"
            @verify="onCaptchaVerified"
            @expired="resetCaptcha"
            :sitekey="sitekey"
            :loadRecaptchaScript="true"
        />
	 	<button  type="submit">Submit</button>
	</form>
</template>

<script>
import VueRecaptcha from 'vue-recaptcha'

export default {
  components: {
    VueRecaptcha
  },
  computed: {
    sitekey () {
      return "YOUR_GOOGLE_RECAPTHCA_SITE_KEY";
    }
  },
  methods: {
    onFormSubmit() {
      this.$refs.recaptcha.execute()
    },
    onCaptchaVerified (token) {
      // Prepare form data
      const formData = new FormData(this.$refs.contactform)

      // Appended token in formData
      formData.append('recaptcha-token', token)

      // Make an ajax request to your Laravel endpoint.
      axios.post('/your-form-endpoint', formData)
        .then(response => {
          // handle response
        }, error => {
          // handle errors
        })
    },
    resetCaptcha () {
      this.$refs.recaptcha.reset()
    }
  }
}
</script>

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

google-recaptcha's People

Contributors

iamswap avatar rajeshdewle avatar

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.