@getjoystick/vue-turnstile
Version:
Vue library for Cloudflare Turnstile CAPTCHA alternative
116 lines (115 loc) • 2.87 kB
JavaScript
import { defineComponent as d, openBlock as l, createElementBlock as a } from "vue";
const c = "https://challenges.cloudflare.com/turnstile/v0/api.js", u = "cfTurnstileOnLoad";
let i = window.turnstile !== void 0 ? "ready" : "unloaded", s;
const m = d({
name: "VueTurnstile",
emits: ["update:modelValue"],
props: {
siteKey: {
type: String,
required: !0
},
modelValue: {
type: String,
required: !0
},
resetInterval: {
type: Number,
required: !1,
default: 295 * 1e3
},
size: {
type: String,
required: !1,
default: "normal"
},
theme: {
type: String,
required: !1,
default: "auto"
},
action: {
type: String,
required: !1,
default: ""
},
renderOnMount: {
type: Boolean,
required: !1,
default: !0
},
removeBeforeUnMount: {
type: Boolean,
required: !1,
default: !0
}
},
data() {
return {
timeoutId: void 0,
turnstileWidgetId: void 0
};
},
computed: {
turnstileOptions() {
return {
sitekey: this.siteKey,
theme: this.theme,
size: this.size,
callback: this.callback,
action: this.action
};
}
},
methods: {
callback(r) {
this.$emit("update:modelValue", r), this.startResetTimeout();
},
reset() {
window.turnstile && (this.$emit("update:modelValue", ""), window.turnstile.reset());
},
render() {
this.turnstileWidgetId = window.turnstile.render(this.$refs.turnstile, this.turnstileOptions);
},
remove() {
window.turnstile && this.turnstileWidgetId && window.turnstile.remove(this.turnstileWidgetId), this.timeoutId && clearTimeout(this.timeoutId);
},
startResetTimeout() {
this.timeoutId = setTimeout(() => {
this.reset();
}, this.resetInterval);
}
},
async mounted() {
const r = new Promise((t, e) => {
s = { resolve: t, reject: e }, i === "ready" && t(void 0);
});
window[u] = () => {
s.resolve(), i = "ready";
}, await (() => {
if (i === "unloaded") {
i = "loading";
const t = `${c}?onload=${u}&render=explicit`, e = document.createElement("script");
e.src = t, e.async = !0, e.addEventListener("error", () => {
s.reject("Failed to load Turnstile.");
}), document.head.appendChild(e);
}
return r;
})(), this.renderOnMount && this.render();
},
async beforeUnmount() {
this.removeBeforeUnMount && this.remove();
}
}), f = (r, n) => {
const t = r.__vccOpts || r;
for (const [e, o] of n)
t[e] = o;
return t;
}, p = { ref: "turnstile" };
function h(r, n, t, e, o, y) {
return l(), a("div", p, null, 512);
}
const w = /* @__PURE__ */ f(m, [["render", h]]);
export {
w as default
};