adonis-hcaptcha

Integrate hcaptcha into adonis framework

Usage no npm install needed!

<script type="module">
  import adonisHcaptcha from 'https://cdn.skypack.dev/adonis-hcaptcha';
</script>

README

Adonis HCaptcha

Integration hCaptcha into adonis framework.


NOTE:

It works for Validator legacy version (^5.1.0), and not yet tested for adonis v5.


Getting Started

Install the package using the adonis CLI.

> adonis install adonis-hcaptcha

Instruction: (Click Here).

Instructions

Config

The config file is saved as config/hcaptcha.js.

module.exports = {
    siteKey : Env.get('HCAPTCHA_SITE_KEY'),
    secretKey : Env.get('HCAPTCHA_SECRET_KEY'),

    // optional
    theme : Env.get('HCAPTCHA_THEME', 'light'),
    size : Env.get('HCAPTCHA_SIZE', 'normal'),
}

or you can use environment variables that you can set on .env file.

HCAPTCHA_SITE_KEY=mysitekey
HCAPTCHA_SECRET_KEY=mysecretkey
HCAPTCHA_THEME=dark
HCAPTCHA_SIZE=compact

How To Use

on view / template:

<form method="post" action="/login">
    {{ csrfField() }}
    <input type="text" name="email">
    <input type="text" name="password">

    {{ hcaptcha() }}
    <button type="submit">Login</button>
</form>

on controller:

const Hcaptcha = use('Hcaptcha');

class LoginController {
    ...

    async store({ request }) {
        const isValid = await Hcaptcha.verify(
            request.input('h-captcha-response')
        );
        
        if (isValid) {
            // do something
        } else {
            // fail !!!
        }
    }
}

use Validator:

const Hcaptcha = use('Hcaptcha');
const { validate } = use('Validator');

class LoginController {
    ...

    async store({ request }) {
        const rules = {
            'h-captcha-response': 'hcaptcha',
            // Or: 
            [Hcaptcha.hCaptchaFieldName()]: Hcaptcha.hCaptchaRuleName(),
        };

        const validation = await validate(request.all(), rules);
        if (validation.fails()) {
            console.log(validation.messages());
            return 'fail';
        } else {
            return 'success';
        }
    }
}

Authors


Agung Yuliyanto
💻