AltchaValidation.client.vue 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <template>
  2. <altcha-widget
  3. v-show="widget !== null"
  4. ref="widget"
  5. :challengeurl="runtimeConfig.public.challengeUrl"
  6. debug
  7. />
  8. </template>
  9. <script setup lang="ts">
  10. import { ref, onMounted, onUnmounted, type Ref } from 'vue'
  11. import 'altcha'
  12. import { useRuntimeConfig } from 'nuxt/app'
  13. import type { PropType } from '@vue/runtime-core'
  14. const runtimeConfig = useRuntimeConfig()
  15. const appStore = useAppStore()
  16. const widget: Ref<HTMLElement | null> = ref(null)
  17. defineProps({
  18. modelValue: {
  19. type: String as PropType<string | null>,
  20. required: true
  21. }
  22. })
  23. const emit = defineEmits(['update:payload'])
  24. const onStateChange = (e: CustomEvent | Event) => {
  25. if ('detail' in e) {
  26. const { payload, state } = e.detail
  27. if (state === 'verified' && payload) {
  28. appStore.setAltchaPayload(payload)
  29. emit('update:modelValue', payload)
  30. }
  31. }
  32. }
  33. onMounted(() => {
  34. setTimeout(() => {
  35. if (widget.value) {
  36. widget.value.addEventListener('statechange', onStateChange)
  37. }
  38. }, 10)
  39. })
  40. onUnmounted(() => {
  41. if (widget.value) {
  42. widget.value.removeEventListener('statechange', onStateChange)
  43. }
  44. })
  45. </script>
  46. <style scoped lang="scss">
  47. </style>