AltchaValidation.client.vue 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. import type { PropType } from '@vue/runtime-core'
  15. const runtimeConfig = useRuntimeConfig()
  16. const appStore = useAppStore()
  17. // Setup a 15min expiration date for the payload
  18. const expire: number = 15 * 60 * 1000
  19. const widget: Ref<HTMLElement | null> = ref(null)
  20. defineProps({
  21. modelValue: {
  22. type: String as PropType<string | null>,
  23. required: true
  24. }
  25. })
  26. const emit = defineEmits(['update:payload'])
  27. const onStateChange = (e: CustomEvent | Event) => {
  28. if ('detail' in e) {
  29. const { payload, state } = e.detail
  30. if (state === 'verified' && payload) {
  31. appStore.setAltchaPayload(payload)
  32. emit('update:modelValue', payload)
  33. }
  34. }
  35. }
  36. onMounted(() => {
  37. setTimeout(() => {
  38. if (widget.value) {
  39. widget.value.addEventListener('statechange', onStateChange)
  40. }
  41. }, 10)
  42. })
  43. onUnmounted(() => {
  44. if (widget.value) {
  45. widget.value.removeEventListener('statechange', onStateChange)
  46. }
  47. })
  48. </script>
  49. <style scoped lang="scss">
  50. </style>