vue-recaptcha
Version:
ReCAPTCHA vue component
40 lines (39 loc) • 1.26 kB
JavaScript
import { whenever } from "@vueuse/shared";
import { computed, watch } from "vue-demi";
import { RecaptchaV2State, useChallengeV2 } from "./challenge-v2.mjs";
export function useComponentV2(options, modelValue, emit) {
const { root, state, widgetID, onError, onExpired, onVerify, reset, execute } = useChallengeV2({
options: options || {}
});
const isExpired = computed(() => state.value === RecaptchaV2State.Expired);
const isError = computed(() => state.value === RecaptchaV2State.Error);
const isVerified = computed(() => state.value === RecaptchaV2State.Verified);
whenever(widgetID, (id) => {
emit("load", id);
emit("update:widgetId", id);
});
watch(modelValue, (res, old) => {
if (!res && old && !isExpired.value) {
callReset();
}
});
onExpired(() => {
emit("update:modelValue", null);
emit("expired", widgetID.value);
});
onError((err) => {
emit("error", err);
});
onVerify((response) => {
emit("success", response);
emit("update:modelValue", response);
});
return { root, widgetID, state, isError, isExpired, isVerified, reset: callReset, execute };
function callReset() {
reset();
resetState();
}
function resetState() {
emit("update:modelValue", null);
}
}