Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/NerdyLuffy/adonisjs-hcaptcha

A middleware for AdonisJS to keep your web applications secure from bots using hCaptcha
https://github.com/NerdyLuffy/adonisjs-hcaptcha

adonis adonis-framework adonis-hcaptcha adonisjs hcaptcha

Last synced: 3 months ago
JSON representation

A middleware for AdonisJS to keep your web applications secure from bots using hCaptcha

Awesome Lists containing this project

README

        




adonisjs-hcaptcha



A package to keep your AdonisJS applications safe from bots, spam and protect your user privacy



[![github-actions-image]][github-actions-url] [![npm-image]][npm-url] [![license-image]][license-url] ![][typescript-image] [![synk-image]][synk-url]


Installation


Install and configure the package in your Adonis project.

```bash
# npm
npm i adonisjs-hcaptcha
node ace configure adonisjs-hcaptcha

# yarn
yarn add adonisjs-hcaptcha
node ace configure adonisjs-hcaptcha
```


Usage

Step 1: Registration

Signup for a account on [hCaptcha website](https://www.hcaptcha.com/signup-interstitial)
Login and follow the steps to get your secret and site key

Step 2: Add variables in `.env` file

```txt
HCAPTCHA_SECRET_KEY=YOUR_SECRET_KEY
HCAPTCHA_SITE_KEY=YOUR_SITE_KEY
```

Step 3: Add validation in the `.env.ts` file

```ts
import Env from '@ioc:Adonis/Core/Env'

export default Env.rules({
// ....
HCAPTCHA_SITE_KEY: Env.schema.string(),
HCAPTCHA_SECRET_KEY: Env.schema.string(),
})
```

Step 4: Add middleware to `start/kernel.ts`

```ts
Server.middleware.registerNamed({
// ....
hcaptcha: () => import('App/Middleware/Hcaptcha'),
})
```

Step 5: Add middleware to your route

```ts
Route.post('login', 'UserController.login').middleware('hcaptcha')
```
The new middleware will check for `h-captcha-response` field in [request input](https://docs.adonisjs.com/guides/request#requestinput)

> `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

Step 6: Check response in your controller

```ts
export default class UsersController {
public async index({ hcaptcha }: HttpContextContract) {
if (hcaptcha.success) {
// Do some action
}
// Throw error
}
}

```

[github-actions-image]: https://img.shields.io/github/workflow/status/NerdyLuffy/adonisjs-hcaptcha/test?style=for-the-badge
[github-actions-url]: https://github.com/NerdyLuffy/adonisjs-hcaptcha/actions/workflows/test.yml "github-actions"

[npm-image]: https://img.shields.io/npm/v/adonisjs-hcaptcha.svg?style=for-the-badge&logo=npm
[npm-url]: https://npmjs.org/package/adonisjs-hcaptcha "npm"

[license-image]: https://img.shields.io/npm/l/adonisjs-hcaptcha?color=blueviolet&style=for-the-badge
[license-url]: LICENSE.md "license"

[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
[typescript-url]: "typescript"

[synk-image]: https://img.shields.io/snyk/vulnerabilities/github/NerdyLuffy/adonisjs-hcaptcha?style=for-the-badge
[synk-url]: https://snyk.io/test/github/NerdyLuffy/adonisjs-hcaptcha?targetFile=package.json "synk"