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

https://github.com/maaaathis/turnstile-verify

Simple server-side implementation for validating Cloudflare Turnstile captchas
https://github.com/maaaathis/turnstile-verify

Last synced: about 2 months ago
JSON representation

Simple server-side implementation for validating Cloudflare Turnstile captchas

Awesome Lists containing this project

README

        

# Turnstile Verify

This npm package provides a simple server-side implementation for validating [Cloudflare Turnstile](https://challenges.cloudflare.com/) captchas.

## Installation

```shell
npm i turnstile-verify
```

## Example usage

```javascript
const { TurnstileVerify } = require('turnstile-verify');

// Your Cloudflare Turnstile secret key
const SECRET_KEY = '1x0000000000000000000000000000000AA';

// ...

// Access Cloudflare Turnstile response and remote IP address
const cfTurnstileResponse = req.body['cf-turnstile-response'];
const remoteip =
req.headers['CF-Connecting-IP'] ||
req.headers['x-forwarded-for'] ||
req.socket.remoteAddress; // optional

// Create a Turnstile instance with the secret key
const turnstile = new TurnstileVerify({ token: SECRET_KEY });

// Validate the Turnstile response
const turnstileResponse = await turnstile.validate({
response: cfTurnstileResponse,
remoteip,
});

// Handle an invalid captcha response
if (!turnstileResponse.valid) {
// ....Handle invalid captcha response
}

// ...
```

### Response

```
{
valid: boolean, => Indicates if capcha solved correctly?
messages: string[] => In case of an error the messages from cloudflare are passed here.
}
```

## Client side implementation

For client side implementaion see [official documentation](https://developers.cloudflare.com/turnstile/get-started/client-side-rendering/), [react-turnstile](https://www.npmjs.com/package/react-turnstile) or use other npm package suitable for your framework.

## License

[MIT](https://choosealicense.com/licenses/mit/)