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

Cannot destructure property 'executeRecaptcha' #682

Open
gkatsanos opened this issue Feb 28, 2024 · 9 comments
Open

Cannot destructure property 'executeRecaptcha' #682

gkatsanos opened this issue Feb 28, 2024 · 9 comments

Comments

@gkatsanos
Copy link

I'm using the plugin in a Vue component but inside an Astro app. Not sure if that plays a role. Here's what I'm getting as an error:

<script setup lang="ts">

import { VueReCaptcha, useReCaptcha } from "vue-recaptcha-v3"
const { executeRecaptcha, recaptchaLoaded } = useReCaptcha()

const recaptcha = async () => {
  // (optional) Wait until recaptcha has been loaded.
  await recaptchaLoaded()

  // Execute reCAPTCHA with action "login".
  const token = await executeRecaptcha("login")

  console.log("token", token)
}

</script>
12:25:26 [ERROR] Cannot destructure property 'executeRecaptcha' of '__vite_ssr_import_9__.useReCaptcha(...)' as it is undefined.
@dayhi
Copy link

dayhi commented Mar 5, 2024

同样的问题,你的解决了吗

@Akulko
Copy link

Akulko commented Mar 6, 2024

same issue

@mcorraodab
Copy link

mcorraodab commented Mar 6, 2024

Just installed the library, same issue here.

I believe the problem is that executing useReCaptcha() returns "undefined"

@zapaza
Copy link

zapaza commented Apr 15, 2024

same issue

@xfudox
Copy link

xfudox commented Jun 25, 2024

Same issue here, can confirm what @mcorraodab said, useRecaptcha() returns a null, so docs for vue3 composition api are wrong.

Anyone solved this?

@cedroux
Copy link

cedroux commented Aug 6, 2024

Solution :

import { useReCaptcha } from 'vue-recaptcha-v3';

const reCaptcha = useReCaptcha();

const login = async () => {
  await reCaptcha.recaptchaLoaded();

  const token = await reCaptcha.executeRecaptcha('login');

  console.log('token', token);
}

@jackkitley
Copy link

Get this logging in SSR for that @cedroux

injection "Symbol(VueReCaptchaInjectKey)" not found.

@ryancrumble
Copy link

ryancrumble commented Nov 22, 2024

Solution is based on this article

@jackkitley mentioned it is occurring in SSR. This approach utilizes the onMounted, which is only called client side.

// googleRecaptcha.composable.ts

import { type IReCaptchaComposition, useReCaptcha } from 'vue-recaptcha-v3'

export function useGoogleRecaptcha() {
  const recaptchaInstance = ref<IReCaptchaComposition | undefined>()

  onMounted(() => {
    recaptchaInstance.value = useReCaptcha()
  })

  async function executeRecaptcha(action: string) {
    if (!recaptchaInstance.value) {
      throw new Error('Recaptcha instance is not available')
    }

    await recaptchaInstance.value.recaptchaLoaded()

    return recaptchaInstance.value.executeRecaptcha(action)
  }

  return executeRecaptcha
}
// nuxtRecaptchaPlugin.client.ts

import { VueReCaptcha } from 'vue-recaptcha-v3'
import type { IReCaptchaOptions } from 'vue-recaptcha-v3/dist/IReCaptchaOptions'


export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.hook('app:mounted', () => {
    const options: IReCaptchaOptions = {
      siteKey: {GOOGLE_RECAPTCHA_KEY},
      loaderOptions: {
        ...
      }
    }

    nuxtApp.vueApp.use(VueReCaptcha, options)
  })
})

@nsvk13
Copy link

nsvk13 commented Feb 21, 2025

Solution is based on this article

@jackkitley mentioned it is occurring in SSR. This approach utilizes the onMounted, which is only called client side.

// googleRecaptcha.composable.ts

import { type IReCaptchaComposition, useReCaptcha } from 'vue-recaptcha-v3'

export function useGoogleRecaptcha() {
const recaptchaInstance = ref<IReCaptchaComposition | undefined>()

onMounted(() => {
recaptchaInstance.value = useReCaptcha()
})

async function executeRecaptcha(action: string) {
if (!recaptchaInstance.value) {
throw new Error('Recaptcha instance is not available')
}

await recaptchaInstance.value.recaptchaLoaded()

return recaptchaInstance.value.executeRecaptcha(action)

}

return executeRecaptcha
}

// nuxtRecaptchaPlugin.client.ts

import { VueReCaptcha } from 'vue-recaptcha-v3'
import type { IReCaptchaOptions } from 'vue-recaptcha-v3/dist/IReCaptchaOptions'

export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.hook('app:mounted', () => {
const options: IReCaptchaOptions = {
siteKey: {GOOGLE_RECAPTCHA_KEY},
loaderOptions: {
...
}
}

nuxtApp.vueApp.use(VueReCaptcha, options)

})
})

okay, but i disabled SSR, and the error again.
is there any other way?

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

10 participants