From e8aea0ece70787860db0508b112b79e2f0669c90 Mon Sep 17 00:00:00 2001 From: saparatayev Date: Thu, 1 Dec 2022 12:08:50 +0500 Subject: [PATCH] recaptcha v2 --- package.json | 1 + src/views/register/Main.vue | 90 +++++++++++++++++++++++++------------ 2 files changed, 63 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index cef5085..846c8f7 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "tom-select": "^1.7.5", "vue": "^3.2.25", "vue-i18n": "9", + "vue-recaptcha": "^2.0.3", "vue-router": "4", "xlsx": "^0.16.9", "zoom-vanilla.js": "^2.0.6" diff --git a/src/views/register/Main.vue b/src/views/register/Main.vue index 958afb0..6485cb2 100644 --- a/src/views/register/Main.vue +++ b/src/views/register/Main.vue @@ -109,9 +109,17 @@ +
+ +
@@ -158,12 +166,15 @@ import LogoInfo from "@/components/logo-info/Main.vue"; import Lang from "@/components/lang/Main.vue"; import i18nn from "@/i18n"; import { ACCOUNT_TYPE_BUSINESS, RECAPTCHA_SCORE_MIN, RECAPTCHA_ACTION } from "@/helpers"; -import { load } from 'recaptcha-v3'; +// import { load } from 'recaptcha-v3'; import { fetchWrapper } from "@/api"; +import { VueRecaptcha } from 'vue-recaptcha'; const reCaptchaUrl = import.meta.env.VITE_RECAPTCHA_URL; -const reCaptchaSecret = import.meta.env.VITE_RECAPTCHA_SECRET; -const reCaptchaKey = import.meta.env.VITE_RECAPTCHA_KEY; +// const reCaptchaSecret = import.meta.env.VITE_RECAPTCHA_SECRET; +// const reCaptchaKey = import.meta.env.VITE_RECAPTCHA_KEY; +const reCaptchaV2Key = import.meta.env.VITE_RECAPTCHA_V2_KEY; +const reCaptchaV2Secret = import.meta.env.VITE_VITE_RECAPTCHA_V2_SECRET; const countriesStore = useCountriesStore(); const alertStore = useAlertStore(); @@ -171,6 +182,7 @@ const alertStore = useAlertStore(); const { countries } = storeToRefs(countriesStore); const isLoading = ref(false); +const recaptchaVerified = ref(false); const formData = reactive({ firstname: "", @@ -179,15 +191,37 @@ const formData = reactive({ password: "", country: "", accountType: "", - recaptchaToken: "" + // recaptchaToken: "" }); const recaptchaInstance = ref(null); -const recaptcha = async () => { - recaptchaInstance.value = await load(reCaptchaKey); - formData.recaptchaToken = await recaptchaInstance.value.execute(RECAPTCHA_ACTION); -} +const handleError = (error) => { + console('recaptcha error'); + console(error); + recaptchaVerified.value = false; + refreshInFiveSeconds(); + alert('Recaptcha Error: The page will be refreshed in 5 seconds.'); +}; + +const handleSuccess = async (response) => { + console.log('recaptcha response'); + console.log(response); + + // let recaptchaResponse = await fetchWrapper.post(reCaptchaUrl, { + // secret: reCaptchaV2Secret, + // response: response + // }); + + // console.log(recaptchaResponse); + + recaptchaVerified.value = true; +}; + +// const recaptcha = async () => { +// recaptchaInstance.value = await load(reCaptchaKey); +// formData.recaptchaToken = await recaptchaInstance.value.execute(RECAPTCHA_ACTION); +// } const rules = { firstname: { @@ -235,9 +269,9 @@ const onRegister = async () => { validate.value.$touch(); // recaptcha connection error or other - if(formData.recaptchaToken === "") { - refreshInFiveSeconds(); - } + // if(formData.recaptchaToken === "") { + // refreshInFiveSeconds(); + // } // if form is valid if (!validate.value.$invalid) { @@ -252,7 +286,7 @@ const onRegister = async () => { isLoading.value = true; - let recaptchaResponse = null; + // let recaptchaResponse = null; // try { // // validate reCaptcha @@ -288,20 +322,20 @@ const onRegister = async () => { } }; -const recaptchaVerifiedSuccessfully = (recaptchaResponse) => { +// const recaptchaVerifiedSuccessfully = (recaptchaResponse) => { - if(recaptchaResponse["success"]) { +// if(recaptchaResponse["success"]) { - if(recaptchaResponse["score"] > RECAPTCHA_SCORE_MIN) { +// if(recaptchaResponse["score"] > RECAPTCHA_SCORE_MIN) { - return recaptchaResponse["action"] === RECAPTCHA_ACTION; - } +// return recaptchaResponse["action"] === RECAPTCHA_ACTION; +// } - return false; - } +// return false; +// } - return false; -} +// return false; +// } const onLogin = () => router.push({ path: "/login" }); @@ -325,14 +359,14 @@ onBeforeMount(async () => { // if (tm !== undefined) formData.country = tm.id // get recaptcha token - await recaptcha(); - console.log("recaptcha token: " + formData.recaptchaToken); - recaptchaInstance.value.showBadge(); + // await recaptcha(); + // console.log("recaptcha token: " + formData.recaptchaToken); + // recaptchaInstance.value.showBadge(); }); -onBeforeUnmount(async () => { - recaptchaInstance.value.hideBadge(); -}); +// onBeforeUnmount(async () => { +// recaptchaInstance.value.hideBadge(); +// });