npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nuxtjs/turnstile

v0.9.11

Published

Cloudflare Turnstile integration for Nuxt

Downloads

23,871

Readme

Nuxt Turnstile

npm version npm downloads Github Actions Codecov

Cloudflare Turnstile integration for Nuxt 3

Features

  • 💪 smart verification with minimal user interaction
  • 🕵️‍♀️ privacy-focused approach
  • ✨ server validation helper for your nitro endpoints
  • ⚡️ lightweight - script only loaded when required

Installation

  1. First, follow these steps to obtain a free sitekey and secret key from Cloudflare.

  2. Install @nuxt/scripts as a dependency - see docs for more info if you're interested.

  3. Install and add @nuxtjs/turnstile to your nuxt.config.

    npx nuxi@latest module add turnstile
    export default defineNuxtConfig({
      modules: ['@nuxtjs/turnstile'],
    
      turnstile: {
        siteKey: '<your-site-key>',
      },
    
      runtimeConfig: {
        turnstile: {
          // This can be overridden at runtime via the NUXT_TURNSTILE_SECRET_KEY
          // environment variable.
          secretKey: '',
        },
      },
    })

Alternatively, you may set turnstile.secretKeyPath to a path to a file containing the secret key. This will be read at build-time and will override any other explicit secretKey you have set.

Tip: At runtime you can override site and secret keys with the NUXT_TURNSTILE_SECRET_KEY and NUXT_PUBLIC_TURNSTILE_SITE_KEY environment variables.

Usage

To use Turnstile, you will likely want to:

  • Use the <NuxtTurnstile> component in your app (for example to build a contact form)
  • Verify the token on your server, when you are processing an API request or a form submission (for example, before sending the email out)

Client

To use Turnstile, add the auto-imported Vue component in whatever component needs it:

<template>
  <div>
    <form @submit.prevent="onSubmit">
      <NuxtTurnstile v-model="token" />
      <input type="submit" />
    </form>
  </div>
</template>

<NuxtTurnstile> can take a number of options via the options argument. See all options. It renders the Turnstile <iframe> within a <div> wrapper by default, but you can configure this by setting the element prop.

When in the page, it will automatically load the Turnstile script and validate your user. Each validation lasts for 300s, and @nuxtjs/turnstile will automatically revalidate this token after 250s.

You can access the validation token by setting a v-model on the component. Then, send the token along with your form responses, either explicitly or automatically (Cloudflare adds a hidden form element with the name cf-turnstile-response to your form). To validate the token on server-side, you can use the auto-imported verifyTurnstileToken utility in your Nitro server routes.

The turnstile token is no longer valid after being processed with CloudFlare via verifyTurnstileToken. If you are using @nuxtjs/turnstile with a component that might need to be validated multiple times, such as a submission form, you will need to regenerate the token for each submission. To manually regenerate the token, @nuxtjs/turnstile exposes the reset function directly via a template ref.

Example:

<template>
  <NuxtTurnstile ref="turnstile" />
  <button @click="turnstile.reset()">Reset token in template</button>
  <button @click="reset()">Reset token from handler</button>
</template>

<script setup>
  // you can call this template ref anything
  const turnstile = ref()

  function reset() {
    turnstile.value?.reset()
  }
</script>

Server

You can either use the a generated validation endpoint, or use the imported helper method:

Example with endpoint:

Turn on the generation of the endpoint first:

export default defineNuxtConfig({
  // ...
  turnstile: {
    siteKey: '<your-site-key>',
    addValidateEndpoint: true
  },
})

You can now call the endpoint at /_turnstile/validate from the client to validate tokens.

Example with custom endpoint and helper method:

// server/api/validateTurnstile.ts

export default defineEventHandler(async (event) => {
  const { token } = await readBody(event)

  if (!token) {
    throw createError({
      statusCode: 422,
      statusMessage: 'Token not provided.',
    })
  }

  return await verifyTurnstileToken(token)
})

💻 Development

  • Clone this repository
  • Enable Corepack using corepack enable
  • Install dependencies using pnpm install
  • Stub module with pnpm dev:prepare
  • Run pnpm dev to start playground in development mode

Credits

License

Made with ❤️

Published under the MIT License.