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

Token is null #80

Open
kerimkuscu opened this issue Apr 27, 2021 · 3 comments
Open

Token is null #80

kerimkuscu opened this issue Apr 27, 2021 · 3 comments

Comments

@kerimkuscu
Copy link

I added the following as a vue file in the Vue cli project.

<template>
  <div :id="id" />
</template>

<script>
export default {
  name: 'GoogleRecaptcha',
  props: {
    action: {
      type: String,
      required: false,
      default: 'validate_grecaptcha'
    },
    id: {
      type: String,
      required: false,
      default: 'grecaptcha_container'
    },
    siteKey: {
      type: String,
      required: false, // set to true if you don't want to store the siteKey in this component
      default: '' // set siteKey here if you want to store it in this component
    },
    inline: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  data() {
    return {
      captchaId: null,
    }
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      if (!document.getElementById('gRecaptchaScript')) {
        window.gRecaptchaOnLoadCallbacks = [this.render];
        window.gRecaptchaOnLoad = function () {
          for (let i = 0; i < window.gRecaptchaOnLoadCallbacks.length; i++) {
            window.gRecaptchaOnLoadCallbacks[i]();
          }
          delete window.gRecaptchaOnLoadCallbacks;
          delete window.gRecaptchaOnLoad;
        };
        let recaptchaScript = document.createElement('script');
        recaptchaScript.setAttribute('src', 'https://www.google.com/recaptcha/api.js?render=explicit&onload=gRecaptchaOnLoad');
        recaptchaScript.setAttribute('id', 'gRecaptchaScript');
        recaptchaScript.async = true;
        recaptchaScript.defer = true;
        document.head.appendChild(recaptchaScript);
      } else if (!window.grecaptcha || !window.grecaptcha.render) {
        window.gRecaptchaOnLoadCallbacks.push(this.render);
      } else {
        this.render();
      }
    },
    render() {
      this.captchaId = window.grecaptcha.render(this.id, {
        sitekey: this.siteKey,
        badge: this.inline === true ? 'inline' : '',
        size: 'invisible',
        'expired-callback': this.execute
      });
      this.execute();
    },
    execute() {
      window.grecaptcha.execute(this.captchaId, {
        action: this.action,
      }).then((token) => {
        this.$emit('input', token);
      });
    }
  }
}
</script>

Then I imported the vue file. I used the vue component, whose name is google-recaptcha, as follows.

<div>
            <google-recaptcha
              id="contact_us_id"
              ref="captcha"
              v-model="form.recaptcha"
              :site-key="siteKey"
              inline
              action="contact_us"
            />
          </div>

siteKey is 6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI

While the page is loading, the following requests are dropped.
Screen Shot 2021-04-27 at 22 28 45

But while these requests are being sent, the input event does not receive your token.
Screen Shot 2021-04-27 at 22 30 11

After waiting for a while, it sends the reload request again and takes the token here. How can I get the token when first making these requests.

@RyanDaDeng
Copy link
Owner

checking tonight

@Luigidefra
Copy link

same error, token is null
I work on vue3 but i m not using api composition

@AGNICO
Copy link

AGNICO commented Nov 21, 2021

I had the same error. Try to change execute() method in GoogleReCaptchaV3.vue component (line 80) to:

execute() {
    window.grecaptcha.execute(this.captchaId, {
        action: this.action,
    }).then((token) => {
        this.$emit('update:modelValue', token); // this line
    });
}

See docs...

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

4 participants