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

react-next-captcha

v1.1.6

Published

A customizable captcha package for use with Next.js and React.

Downloads

52

Readme

react-next-captcha

react-next-captcha License

A simple and customizable captcha generator and verifier for Node.js, designed to be used with React and Next.js applications. Built on top of svg-captcha, it provides an easy way to add captchas to your web application without the need for Google reCAPTCHA or C++ addons.

Features

  • Generates SVG captchas
  • Customizable options for captcha size, noise, and characters
  • Secure verification with custom salts
  • Easy integration with Next.js and React

Installation

Install the package using npm:

npm install react-next-captcha

Basic Setup - Next.js

A) Server-Side (Next.js API Routes)

  1. Create a file pages/api/generate-captcha.js:

    import { createCaptcha } from 'react-next-captcha';
    
    export default function handler(req, res) {
         
    
      const options = {
        size: size || 6,
        noise: noise || 2,
        ignoreChars: ignoreChars || '0o1ilIL',
      };
    
      // Generate a custom salt for increased security
      function generateRandomString(length) {
        const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+[]{}|;:,.<>?';
        let result = '';
        const charactersLength = characters.length;
    
        for (let i = 0; i < length; i++) {
          result += characters.charAt(Math.floor(Math.random() * charactersLength));
        }
    
        return result;
      }
      const customSalt = generateRandomString(20);
    
      const captcha = createCaptcha(options, customSalt);
    
      res.status(200).json({ captcha: captcha.data, hash: captcha.hash });
    }
  2. Create a file pages/api/verify-captcha.js:

    import { checkCaptcha } from 'react-next-captcha';
    
    export default function handler(req, res) {
      if (req.method === 'POST') {
        const { text, hash } = req.body;
    
        const verificationCaptcha = checkCaptcha(text, hash);
    
        if (verificationCaptcha.success === true) {
          // Proceed with login logic here
    
          res.status(200).json(verificationCaptcha);
        } else {
          res.status(400).json(verificationCaptcha);
        }
      }
    }

B) Client-Side (React Component)

  1. Create a login component pages/login.js:

    import { useState, useEffect } from 'react';
    
    const Login = () => {
      const [captcha, setCaptcha] = useState('');
      const [hash, setHash] = useState('');
      const [username, setUsername] = useState('');
      const [password, setPassword] = useState('');
      const [captchaInput, setCaptchaInput] = useState('');
      const [message, setMessage] = useState('');
    
      useEffect(() => {
        fetchCaptcha();
      }, []);
    
      const fetchCaptcha = async () => {
        const req = await fetch('/api/generate-captcha', {
          method: "POST",
            
        });
        const res = await req.json();
        setCaptcha(res.captcha);
        setHash(res.hash);
      };
    
      const handleSubmit = async (e) => {
        e.preventDefault();
        const req = await fetch('/api/verify-captcha', {
          method: "POST",
          headers: {
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({ text: captchaInput, hash }),
        });
        const res = await req.json();
        if (res.success) {
          // Proceed with login logic here
          setMessage('Captcha verified');
          fetchCaptcha();
        } else {
          setMessage('Captcha verification failed. Please try again.');
          //fetchCaptcha();
        }
      };
    
      return (
        <div>
          <h1>Login</h1>
          <form onSubmit={handleSubmit}>
            <div>
              <label>
                Username:
                <input
                  type="text"
                  value={username}
                  onChange={(e) => setUsername(e.target.value)}
                  required
                />
              </label>
            </div>
            <div>
              <label>
                Password:
                <input
                  type="password"
                  value={password}
                  onChange={(e) => setPassword(e.target.value)}
                  required
                />
              </label>
            </div>
            <div>
              <div dangerouslySetInnerHTML={{ __html: captcha }} />
              <button type="button" onClick={fetchCaptcha}>Reload Captcha</button>
            </div>
            <div>
              <label>
                Captcha:
                <input
                  type="text"
                  value={captchaInput}
                  onChange={(e) => setCaptchaInput(e.target.value)}
                  required
                />
              </label>
            </div>
            {message && <p style={{ color: 'blue' }}>{message}</p>}
            <div>
              <button type="submit">Login</button>
            </div>
          </form>
        </div>
      );
    };
    
    export default Login;

Advanced Settings

You can customize the captcha generation by passing additional options. Here are some of the options available:

  • size: Number of characters in the captcha (default: 6)
  • noise: Number of noise lines (default: 2)
  • ignoreChars: Characters to ignore in the captcha (default: '0o1ilIL')
  • color: Use distinct colors for characters (default: true if background option is set)
  • background: Background color of the captcha image

Example:

const options = {
  size: 6,
  noise: 3,
  ignoreChars: '0o1ilIL',
  color: true,
  background: '#cc9966',
};
const captcha = createCaptcha(options, customSalt);

API Reference

createCaptcha(options, customSalt)

  • options: Object containing the captcha generation options.
  • customSalt: Custom salt to be used for generating the hash.

Returns an object with the following properties:

  • data: The SVG captcha data.
  • text: The captcha text.
  • hash: The SHA-256 hash of the captcha text combined with the salt.

checkCaptcha(text, hash)

  • text: The text entered by the user.
  • hash: The hash of the captcha.

Returns an object with the following properties:

  • success: Boolean indicating if the verification was successful.
  • message: Verification message.

License

This project is licensed under the ISC License.


This `README.md` file includes the basic setup for Next.js and also highlights how to use and customize the captcha generator and verifier in your application.