Coder Social home page Coder Social logo

douglasjunior / react-recaptcha-that-works Goto Github PK

View Code? Open in Web Editor NEW
16.0 3.0 3.0 114 KB

⚛ A reCAPTCHA bridge for React that works.

Home Page: https://www.npmjs.com/package/react-recaptcha-that-works

License: MIT License

TypeScript 93.56% JavaScript 6.44%
react recaptcha kiss hacktoberfest

react-recaptcha-that-works's Introduction

reCAPTCHA for React

License MIT npm version npm downloads

A reCAPTCHA library for React that works.

Looking for React Native version?

Install

Install the module

  yarn add react-recaptcha-that-works

Or

  npm i -S react-recaptcha-that-works

Import the reCAPTCHA script

<html>
    <head>
        <script src="https://www.google.com/recaptcha/api.js" async defer></script>
    </head>
    <body>
        ...
    </body>
</html>

Usage

I'm not a robot

import React, { Component } from 'react';

import Recaptcha from 'react-recaptcha-that-works';

class App extends Component {

    send = () => {
        console.log('send!', this.state.token);
    }

    onVerify = token => {
        console.log('success!', token);
        this.setState({ token });
    }

    onExpire = () => {
        console.warn('expired!');
    }

    render() {
        return (
            <div>
                <input type="text" placeholder="Username" />
                <input type="password" placeholder="Password" />
                <Recaptcha
                    siteKey="<your-recaptcha-public-key>"
                    onVerify={this.onVerify}
                    onExpire={this.onExpire}
                />
                <button onClick={this.send}>Send</button>
            </div>
        )
    }
}

Invisible

import React, { Component } from 'react';

import Recaptcha from 'react-recaptcha-that-works';

class App extends Component {

    send = () => {
        console.log('send!');
        this.recaptcha.execute();
    }

    onVerify = token => {
        console.log('success!', token);
    }

    onExpire = () => {
        console.warn('expired!');
    }

    render() {
        return (
            <div>
                <input type="text" placeholder="Username" />
                <input type="password" placeholder="Password" />
                <Recaptcha
                    ref={ref = this.recaptcha = ref}
                    siteKey="<your-recaptcha-public-key>"
                    onVerify={this.onVerify}
                    onExpire={this.onExpire}
                    size="invisible"
                />
                <button onClick={this.send}>Send</button>
            </div>
        )
    }
}

TypeScript

import React, { useRef } from 'react';

import Recaptcha, { RecaptchaRef } from "react-recaptcha-that-works";

// ...

export const Component: React.FC = () => {
    const recaptcha = useRef<RecaptchaRef>(null);

    const send = () => {
        console.log('send!');
        recaptcha.current?.execute();
    };

    const onVerify = (token: string) => {
        console.log('success!', token);
    };

    const onExpire = () => {
        console.warn('expired!');
    }

    return (
        <div>
            <Recaptcha
                ref={recaptcha}
                siteKey="<your-recaptcha-public-key>"
                onVerify={onVerify}
                onExpire={onExpire}
                size="invisible"
            />
            <button onClick={send}>
                Send
            </button>
        </div>
    );
};

For more details, see the Sample Project.

Props

Name Value Default Description
siteKey Your sitekey.
size 'invisible', 'normal' or 'compact' 'normal' The size of the widget.
theme 'dark' or 'light' 'light' The color theme of the widget.
onLoad function() A callback function, executed when the reCAPTCHA is ready to use.
onVerify function(token) A callback function, executed when the user submits a successful response. The reCAPTCHA response token is passed to your callback.
onExpire function() A callback function, executed when the reCAPTCHA response expires and the user needs to re-verify.
onError function() A callback function, executed when reCAPTCHA encounters an error (usually network connectivity) and cannot continue until connectivity is restored. If you specify a function here, you are responsible for informing the user that they should retry.
onClose function() (Experimental) A callback function, executed when the challenge window is closed.

React Ref Methods

Name Type Description
execute function Executes the challenge in "invisible" mode.
reset function Resets the reCAPTCHA state.

reCAPTCHA v2 docs

Contribute

New features, bug fixes and improvements are welcome! For questions and suggestions, use the issues.

Become a Patron! Donate

License

The MIT License (MIT)

Copyright (c) 2018 Douglas Nassif Roma Junior

See the full license file.

react-recaptcha-that-works's People

Contributors

douglasjunior avatar moroshko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

react-recaptcha-that-works's Issues

Recaptcha Cannot read properties of undefined errors

  • Browser - google chrome
  • "react": "^18.2.0",
  • react-recaptcha-that-works: "^2.0.1",
  • Did the problem happen after updating the library? - No
  • Are you using the library for the first time? - yes
  • It's a bug? - I have an error: TypeError: Cannot read properties of undefined (reading 'clients') (the first screenshot) after successfully solving Recaptcha I call nextjs router method.push -> router.push(url, undefined, { shallow: true }) and got it.

Investigated source code I found a solution -> unmount before using router.push -> but if I after that I try to come back to prev page I have another error TypeError: Cannot read properties of undefined (reading 'badge') (the second screenshot)

How can I overcome it?

image image

After upgrade to 2.0.0 Error for resolving

Module not found: Can't resolve './Recaptcha'
Did you mean 'Recaptcha.js'?
BREAKING CHANGE: The request './Recaptcha' failed to resolve only because it was resolved as fully specified
(probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request.
> 1 | import Recaptcha from './Recaptcha';
  2 | export default Recaptcha;

`onClose` callback not working

Whenever I try using onClose callback in my recaptcha, it fails because there is no parent node for recaptcha challenge iframe.

To be more specific, the problem is that when the app searches for this window's iframe, this object was not already created.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.