Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

verifyCallback not called #228

Open
PaulRBerg opened this issue Mar 24, 2018 · 12 comments
Open

verifyCallback not called #228

PaulRBerg opened this issue Mar 24, 2018 · 12 comments

Comments

@PaulRBerg
Copy link

PaulRBerg commented Mar 24, 2018

On many occasions, verifyCallback is not called. I don't think it's a deterministic process, but it always happens after the recaptcha asks the user to filter some images.

@PaulRBerg PaulRBerg changed the title verifyCallback verifyCallback not called Mar 24, 2018
@rshk
Copy link

rshk commented Apr 2, 2018

It was behaving slightly differently in my case (Gatsby website), in that the callback was only getting called if landing on the form from another page, not if accessing the form page directly 😕

Possibly some bug hiding somewhere in the isReady() polling? Btw, I noticed state being mutated directly here: https://github.com/appleboy/react-recaptcha/blob/master/src/index.js#L103 -> which usually causes things to break, although I doubt that's what's causing this issue...

Anyways, after copy-pasting the code down and starting to dissect it to pinpoint the issue, I ended up using this stripped-down version, which works a charm for me:

import React, {Component} from 'react';
import PropTypes from 'prop-types';


// Wait for the recaptcha object to become available
// The promise will resolve as soon as needed, keep resolving with the
// cached object.
const waitForRecaptcha = new Promise(resolve=> {
    let interval = setInterval(()=> {
        if (typeof window !== 'undefined' && typeof window.grecaptcha !== 'undefined') {
            clearInterval(interval);
            resolve(window.grecaptcha);
        }
    }, 1000);
});


export default class Recaptcha extends Component {

    constructor(props) {
        super(props);
        this.state = {widget: null};
        this._containerRef = null;
    }

    componentDidMount() {
        this._renderGrecaptcha();
    }

    _renderGrecaptcha() {
        const {sitekey, theme, type, size, tabindex, hl, badge,
               verifyCallback, expiredCallback, onloadCallback} = this.props;

        waitForRecaptcha.then(grecaptcha=> {
            const widget = grecaptcha.render(this._containerRef, {
                sitekey, theme, type, size, tabindex, hl, badge,
                callback: verifyCallback,
                'expired-callback': expiredCallback,
            });
            this.setState({widget});
        });

        if (onloadCallback) {
            onloadCallback();
        }
    }

    render() {
        return <div ref={el=> this._containerRef = el} />;
    }
}


Recaptcha.propTypes = {
    onloadCallback: PropTypes.func,
    verifyCallback: PropTypes.func,
    expiredCallback: PropTypes.func,
    sitekey: PropTypes.string.isRequired,
    theme: PropTypes.string,
    type: PropTypes.string,
    size: PropTypes.string,
    tabindex: PropTypes.string,
    hl: PropTypes.string,
    badge: PropTypes.string,
};


Recaptcha.defaultProps = {
    onloadCallback: undefined,
    verifyCallback: undefined,
    expiredCallback: undefined,
    theme: 'light',
    type: 'image',
    size: 'normal',
    tabindex: '0',
    hl: 'en',
    badge: 'bottomright',
};

@ericapply
Copy link

Having same problem. Needed this for redux-form.

@crypto-titan
Copy link

+1

1 similar comment
@pongpisit33009
Copy link

+1

@Jiert
Copy link

Jiert commented Apr 26, 2018

@rshk Thanks for sharing your version, seems to be working for me!

@AlinaPoluykova
Copy link

+1

2 similar comments
@GlennSouthern
Copy link

+1

@cemkiy
Copy link

cemkiy commented May 27, 2018

+1

@jsardev
Copy link

jsardev commented May 28, 2018

The problem is that this library is not following the newest Recaptcha API.

For example: react-recaptcha is basing on data-onloadcallbackname and data-verifycallbackname tag attributes for explicit rendering, but these attributes don't exist anymore. I don't know if this library was based on Recaptcha V1, but there are few properties that are doing completely nothing and these might be the reasons why it doesn't work at all.

I have some spare time tomorrow and I'll try to make this library compatible with the current Recaptcha API so it would work in all the cases described in the docs.

@rshk
Copy link

rshk commented May 28, 2018

Btw, can I ask pretty please people finding this issue in the future to just click the "thumbs up" on the issue description, instead of commenting with just a +1?

@jsardev
Copy link

jsardev commented Jun 13, 2018

Hey guys! It took me a little bit more time to make it happen, but it's here! My approach has some improvements comparing to this lib and most important - it works! 😄 🚀 What's awesome - you can have everything in your React component (not outside in some global variables). Verify callback also works like a charm!

Library: https://github.com/sarneeh/reaptcha
Example: https://sarneeh.github.io/reaptcha/

Hope it helps! 😄 🎉

If you'll encounter any problems, just file an issue - I'm available right now and will fix anything pretty fast. 💃

@Metrakit
Copy link

Metrakit commented Nov 7, 2018

Same mistake for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests