Files
sub2api/frontend/src/components/TurnstileWidget.vue

183 lines
3.7 KiB
Vue
Raw Normal View History

2025-12-18 13:50:39 +08:00
<template>
<div v-if="siteKey" class="turnstile-wrapper">
<div ref="containerRef" class="turnstile-container"></div>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted, onUnmounted, watch } from 'vue'
2025-12-18 13:50:39 +08:00
interface TurnstileRenderOptions {
sitekey: string
callback: (token: string) => void
'expired-callback'?: () => void
'error-callback'?: () => void
theme?: 'light' | 'dark' | 'auto'
size?: 'normal' | 'compact' | 'flexible'
2025-12-18 13:50:39 +08:00
}
interface TurnstileAPI {
render: (container: HTMLElement, options: TurnstileRenderOptions) => string
reset: (widgetId?: string) => void
remove: (widgetId?: string) => void
2025-12-18 13:50:39 +08:00
}
declare global {
interface Window {
turnstile?: TurnstileAPI
onTurnstileLoad?: () => void
2025-12-18 13:50:39 +08:00
}
}
const props = withDefaults(
defineProps<{
siteKey: string
theme?: 'light' | 'dark' | 'auto'
size?: 'normal' | 'compact' | 'flexible'
}>(),
{
theme: 'auto',
size: 'flexible'
}
)
2025-12-18 13:50:39 +08:00
const emit = defineEmits<{
(e: 'verify', token: string): void
(e: 'expire'): void
(e: 'error'): void
}>()
2025-12-18 13:50:39 +08:00
const containerRef = ref<HTMLElement | null>(null)
const widgetId = ref<string | null>(null)
const scriptLoaded = ref(false)
2025-12-18 13:50:39 +08:00
const loadScript = (): Promise<void> => {
return new Promise((resolve, reject) => {
if (window.turnstile) {
scriptLoaded.value = true
resolve()
return
2025-12-18 13:50:39 +08:00
}
// Check if script is already loading
const existingScript = document.querySelector('script[src*="turnstile"]')
2025-12-18 13:50:39 +08:00
if (existingScript) {
window.onTurnstileLoad = () => {
scriptLoaded.value = true
resolve()
}
return
2025-12-18 13:50:39 +08:00
}
const script = document.createElement('script')
script.src = 'https://challenges.cloudflare.com/turnstile/v0/api.js?onload=onTurnstileLoad'
script.async = true
script.defer = true
2025-12-18 13:50:39 +08:00
window.onTurnstileLoad = () => {
scriptLoaded.value = true
resolve()
}
2025-12-18 13:50:39 +08:00
script.onerror = () => {
reject(new Error('Failed to load Turnstile script'))
}
2025-12-18 13:50:39 +08:00
document.head.appendChild(script)
})
}
2025-12-18 13:50:39 +08:00
const renderWidget = () => {
if (!window.turnstile || !containerRef.value || !props.siteKey) {
return
2025-12-18 13:50:39 +08:00
}
// Remove existing widget if any
if (widgetId.value) {
try {
window.turnstile.remove(widgetId.value)
2025-12-18 13:50:39 +08:00
} catch {
// Ignore errors when removing
}
widgetId.value = null
2025-12-18 13:50:39 +08:00
}
// Clear container
containerRef.value.innerHTML = ''
2025-12-18 13:50:39 +08:00
widgetId.value = window.turnstile.render(containerRef.value, {
sitekey: props.siteKey,
callback: (token: string) => {
emit('verify', token)
2025-12-18 13:50:39 +08:00
},
'expired-callback': () => {
emit('expire')
2025-12-18 13:50:39 +08:00
},
'error-callback': () => {
emit('error')
2025-12-18 13:50:39 +08:00
},
theme: props.theme,
size: props.size
})
}
2025-12-18 13:50:39 +08:00
const reset = () => {
if (window.turnstile && widgetId.value) {
window.turnstile.reset(widgetId.value)
2025-12-18 13:50:39 +08:00
}
}
2025-12-18 13:50:39 +08:00
// Expose reset method to parent
defineExpose({ reset })
2025-12-18 13:50:39 +08:00
onMounted(async () => {
if (!props.siteKey) {
return
2025-12-18 13:50:39 +08:00
}
try {
await loadScript()
renderWidget()
2025-12-18 13:50:39 +08:00
} catch (error) {
console.error('Failed to initialize Turnstile:', error)
emit('error')
2025-12-18 13:50:39 +08:00
}
})
2025-12-18 13:50:39 +08:00
onUnmounted(() => {
if (window.turnstile && widgetId.value) {
try {
window.turnstile.remove(widgetId.value)
2025-12-18 13:50:39 +08:00
} catch {
// Ignore errors when removing
}
}
})
2025-12-18 13:50:39 +08:00
// Re-render when siteKey changes
watch(
() => props.siteKey,
(newKey) => {
if (newKey && scriptLoaded.value) {
renderWidget()
}
2025-12-18 13:50:39 +08:00
}
)
2025-12-18 13:50:39 +08:00
</script>
<style scoped>
.turnstile-wrapper {
width: 100%;
}
.turnstile-container {
width: 100%;
min-height: 65px;
}
/* Make the Turnstile iframe fill the container width */
.turnstile-container :deep(iframe) {
width: 100% !important;
}
</style>