AltchaValidation.client.vue 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <template>
  2. <altcha-widget
  3. v-show="widget !== null"
  4. ref="widget"
  5. :challengeurl="runtimeConfig.public.challengeUrl"
  6. :expire="expire"
  7. debug
  8. />
  9. </template>
  10. <script setup lang="ts">
  11. import { ref, onMounted, onUnmounted, type Ref } from 'vue'
  12. import 'altcha'
  13. import { useRuntimeConfig } from 'nuxt/app'
  14. const runtimeConfig = useRuntimeConfig()
  15. const appStore = useAppStore()
  16. // Setup a 15min expiration date for the payload
  17. const expire: number = 15 * 60 * 1000
  18. const emit = defineEmits(['verified'])
  19. const widget: Ref<HTMLElement | null> = ref(null)
  20. const onStateChange = (e: CustomEvent | Event) => {
  21. if ('detail' in e) {
  22. const { payload, state } = e.detail
  23. if (state === 'verified' && payload) {
  24. emit('verified')
  25. appStore.setAltchaPayload(payload)
  26. }
  27. }
  28. }
  29. onMounted(() => {
  30. setTimeout(() => {
  31. if (widget.value) {
  32. widget.value.addEventListener('statechange', onStateChange)
  33. }
  34. }, 10)
  35. })
  36. onUnmounted(() => {
  37. if (widget.value) {
  38. widget.value.removeEventListener('statechange', onStateChange)
  39. }
  40. })
  41. </script>
  42. <style scoped lang="scss">
  43. </style>