Home

Awesome

<div align="center"> <img src="https://res.cloudinary.com/adonisjs/image/upload/q_100/v1558612869/adonis-readme_zscycu.jpg" width="600px"> </div> <div align="center"> <h2>adonisjs-hcaptcha</h2> <p> A package to keep your AdonisJS applications safe from bots, spam and protect your user privacy </p> </div> <br/> <div align="center">

github-actions-image npm-image license-image synk-image

</div> <hr/> <h2> Installation </h2> Install and configure the package in your Adonis project.
# npm
npm i adonisjs-hcaptcha
node ace configure adonisjs-hcaptcha

# yarn
yarn add adonisjs-hcaptcha
node ace configure adonisjs-hcaptcha
<hr/> <h2> Usage </h2> <h3> Step 1: Registration </h3>

Signup for a account on hCaptcha website Login and follow the steps to get your <b>secret</b> and <b>site</b> key

<h3> Step 2: Add variables in `.env` file </h3>
HCAPTCHA_SECRET_KEY=YOUR_SECRET_KEY 
HCAPTCHA_SITE_KEY=YOUR_SITE_KEY
<h3> Step 3: Add validation in the `.env.ts` file </h3>
import Env from '@ioc:Adonis/Core/Env'

export default Env.rules({
  // ....
  HCAPTCHA_SITE_KEY: Env.schema.string(),
  HCAPTCHA_SECRET_KEY: Env.schema.string(),
})
<h3> Step 4: Add middleware to `start/kernel.ts` </h3>
Server.middleware.registerNamed({
  // ....
  hcaptcha: () => import('App/Middleware/Hcaptcha'),
})
<h3> Step 5: Add middleware to your route </h3>
Route.post('login', 'UserController.login').middleware('hcaptcha')

The new middleware will check for h-captcha-response field in request input

h-captcha-response field will contain the unique one time non repeating token which will be validated with hCaptcha to make sure its not a bot

<h3> Step 6: Check response in your controller </h3>
export default class UsersController {
  public async index({ hcaptcha }: HttpContextContract) {
    if (hcaptcha.success) {
      // Do some action
    }
    // Throw error
  }
}