Skip to content

Commit 48d2585

Browse files
fix(recaptcha): new reusable component
1 parent e196a59 commit 48d2585

File tree

2 files changed

+58
-38
lines changed

2 files changed

+58
-38
lines changed

components/Launcher.vue

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,33 @@
11
<template>
22
<v-container justify="space-around">
33
<v-row align-content="center" align="center">
4-
<v-col v-if="!is_captcha_validated" cols="12" align-self="center" align="center">
5-
<h4 class="pb-3">
6-
Please complete the recaptcha to launch the app
7-
</h4>
8-
<vue-recaptcha ref="recaptcha" :sitekey="site_key" :loadRecaptchaScript="true"
9-
@expired="is_captcha_validated = false" @verify="submit_recaptcha" align-self="center" />
4+
<v-col
5+
v-if="!is_captcha_validated"
6+
cols="12"
7+
align-self="center"
8+
align="center"
9+
>
10+
<h4 class="pb-3">Please complete the recaptcha to launch the app</h4>
11+
<Recaptcha />
1012
</v-col>
11-
<v-col v-else-if="!cloud_store.is_running && cloud_store.is_connexion_launched">
13+
<v-col
14+
v-else-if="!cloud_store.is_running && cloud_store.is_connexion_launched"
15+
>
1216
<Loading />
1317
</v-col>
1418
</v-row>
1519
</v-container>
1620
</template>
1721

1822
<script setup>
19-
import { VueRecaptcha } from "vue-recaptcha"
20-
21-
const websocket_store = use_websocket_store()
22-
const cloud_store = use_cloud_store()
23-
const { is_captcha_validated } = storeToRefs(cloud_store)
24-
25-
const props = defineProps({
26-
site_key: { type: String, required: true }
27-
})
23+
const websocket_store = use_websocket_store();
24+
const cloud_store = use_cloud_store();
25+
const { is_captcha_validated } = storeToRefs(cloud_store);
2826
2927
watch(is_captcha_validated, async (value) => {
3028
if (value === true && process.client) {
31-
await cloud_store.create_connexion()
32-
await websocket_store.ws_connect()
33-
}
34-
})
35-
36-
onMounted(() => {
37-
if (process.client) {
38-
const config = useRuntimeConfig()
39-
if (config.public.NODE_ENV !== 'production') {
40-
cloud_store.$patch({ is_captcha_validated: true })
41-
}
42-
}
43-
})
44-
45-
async function submit_recaptcha (token) {
46-
try {
47-
const response = await $fetch.raw(`/.netlify/functions/recaptcha?token=${token}`)
48-
cloud_store.$patch({ is_captcha_validated: response.status == 200 })
49-
recaptcha.reset()
50-
} catch (error) {
51-
console.error(error)
29+
await cloud_store.create_connexion();
30+
await websocket_store.ws_connect();
5231
}
53-
}
32+
});
5433
</script>

components/Recaptcha.vue

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<template>
2+
<vue-recaptcha
3+
ref="recaptcha"
4+
:sitekey="site_key"
5+
:loadRecaptchaScript="true"
6+
@expired="is_captcha_validated = false"
7+
@verify="submit_recaptcha"
8+
align-self="center"
9+
/>
10+
</template>
11+
12+
<script setup>
13+
import { VueRecaptcha } from "vue-recaptcha";
14+
15+
const websocket_store = use_websocket_store();
16+
const cloud_store = use_cloud_store();
17+
const { is_captcha_validated } = storeToRefs(cloud_store);
18+
19+
const site_key = useRuntimeConfig().public.RECAPTCHA_SITE_KEY;
20+
21+
onMounted(() => {
22+
if (process.client) {
23+
const config = useRuntimeConfig();
24+
if (config.public.NODE_ENV !== "production") {
25+
cloud_store.$patch({ is_captcha_validated: true });
26+
}
27+
}
28+
});
29+
30+
async function submit_recaptcha(token) {
31+
try {
32+
const response = await $fetch.raw(
33+
`/.netlify/functions/recaptcha?token=${token}`
34+
);
35+
cloud_store.$patch({ is_captcha_validated: response.status == 200 });
36+
recaptcha.reset();
37+
} catch (error) {
38+
console.error(error);
39+
}
40+
}
41+
</script>

0 commit comments

Comments
 (0)