Coder Social home page Coder Social logo

zhangppbb / recaptcha-module Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nuxt-community/recaptcha-module

0.0 1.0 0.0 302 KB

๐Ÿค– Simple and easy Google reCAPTCHA integration with Nuxt.js

License: MIT License

JavaScript 60.41% Vue 39.59%

recaptcha-module's Introduction

Google reCAPTCHA

npm version npm downloads Circle CI Codecov Standard JS

๐Ÿค– Simple and easy Google reCAPTCHA integration with Nuxt.js

๐Ÿ“– Release Notes

Setup

  1. Add @nuxtjs/recaptcha dependency with yarn or npm into your project
  2. Add @nuxtjs/recaptcha to modules section of nuxt.config.js
  3. Configure it:
{
  modules: [
    [
      '@nuxtjs/recaptcha', {
        /* reCAPTCHA options */
      }
    ],
  ]
}

using top level options

{
  modules: [
    '@nuxtjs/recaptcha',
  ],

  recaptcha: {
    /* reCAPTCHA options */
  },
}

Configuration

{
  // ...
  recaptcha: {
    hideBadge: Boolean, // Hide badge element (v3 & v2 via size=invisible)
    language: String,   // Recaptcha language (v2)
    siteKey: String,    // Site key for requests
    version: Number,     // Version
    size: String        // Size: 'compact', 'normal', 'invisible' (v2)
  },
  // ...
}

Runtime config

// nuxt.config.js
export default {
  publicRuntimeConfig: {
    recaptcha: {
      /* reCAPTCHA options */
      siteKey: process.env.RECAPTCHA_SITE_KEY // for example
    }
  }
}

Usage

reCAPTCHA v2

  1. Add <recaptcha> component inside your form:
<form @submit.prevent="onSubmit">
  <input autocomplete="true" placeholder="Email" type="email" v-model="email">
  <input autocomplete="current-password" placeholder="Password" type="password" v-model="password">
  <recaptcha />
  <button type="submit">Sign In</button>
</form>
  1. Call getResponse inside form submit handler to get reCAPTCHA token:
async onSubmit() {
  try {
    const token = await this.$recaptcha.getResponse()
    console.log('ReCaptcha token:', token)

    // send token to server alongside your form data

    // at the end you need to reset recaptcha
    await this.$recaptcha.reset()
  } catch (error) {
    console.log('Login error:', error)
  }
},

See: v2 example

reCAPTCHA v3

  1. Call init function inside mounted hook of your page
async mounted() {
  try {
    await this.$recaptcha.init()
  } catch (e) {
    console.error(e);
  }
}
  1. Call execute function form submit handler to get reCAPTCHA token:
async onSubmit() {
  try {
    const token = await this.$recaptcha.execute('login')
    console.log('ReCaptcha token:', token)

    // send token to server alongside your form data

  } catch (error) {
    console.log('Login error:', error)
  }
}
  1. Call destroy function inside beforeDestroy hook of the page. (This will remove reCAPTCHA scripts, styles and badge from the page)
beforeDestroy() {
  this.$recaptcha.destroy()
}

See: v3 example

Server Side

When you send data + token to the server, you should verify the token on the server side to make sure it does not requested from a bot. You can find out how to verify token on the server side by looking at the server middleware inside v2 example. (The server side is same for both versions)

Info Hiding Badges

You're allowed to hide the badge (i.e. for v3 and v2 invisible), as long as you include the recaptcha branding in the user flow.

For example:

<small>This site is protected by reCAPTCHA and the Google 
    <a href="https://policies.google.com/privacy">Privacy Policy</a> and
    <a href="https://policies.google.com/terms">Terms of Service</a> apply.
</small>

Development

  1. Clone this repository
  2. Install dependencies using yarn install or npm install
  3. Start development server using npm run dev

License

MIT License

Copyright (c) mvrlin [email protected]

recaptcha-module's People

Contributors

abakermi avatar andrewmurraydavid avatar atinux avatar bason8800 avatar dependabot[bot] avatar dexapiens avatar farnabaz avatar lat1992 avatar mvrlin avatar owenandrews avatar p4sca1 avatar redbayoub avatar sgarner avatar thepoon avatar therealwolf42 avatar

Watchers

 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.