feat: configurable and optional hCaptcha sitekey

This commit is contained in:
niko 2025-04-25 22:28:50 -04:00
parent f6ac4332f9
commit 106370836f
No known key found for this signature in database
GPG Key ID: 54046A9008D73455
2 changed files with 31 additions and 28 deletions

View File

@ -24,7 +24,11 @@ export default defineNuxtConfig({
}, },
runtimeConfig: { runtimeConfig: {
apiBase: 'https://api.pretendo.cc' apiBase: 'https://api.pretendo.cc',
public: {
hCaptchaSitekey: ''
}
}, },
css: ['~/assets/css/main.css'], css: ['~/assets/css/main.css'],

View File

@ -12,35 +12,33 @@ const errorMessage = ref<string | null>();
const invisibleHcaptcha = ref<VueHcaptcha | null>(null); const invisibleHcaptcha = ref<VueHcaptcha | null>(null);
async function registerSubmission() { async function registerSubmission() {
if (invisibleHcaptcha.value) { try {
try { const hCaptchaResponse = invisibleHcaptcha.value ? (await invisibleHcaptcha.value.executeAsync()).response : null;
const hCaptchaResponse = (await invisibleHcaptcha.value.executeAsync()).response;
await $fetch('/api/account/register', { await $fetch('/api/account/register', {
method: 'POST', method: 'POST',
body: { ...registerForm, hCaptchaResponse } body: { ...registerForm, hCaptchaResponse }
}); });
if (typeof redirect.value === 'string') { if (typeof redirect.value === 'string') {
await navigateTo(redirect.value); await navigateTo(redirect.value);
} else { } else {
await navigateTo('/account'); await navigateTo('/account');
}
} catch (error: unknown) {
if (error instanceof FetchError) {
errorMessage.value = error.statusText;
} else {
if (error === 'challenge-closed') { // Thrown if the captcha is closed, can be safely ignored
return;
}
errorMessage.value = `Error during registration: ${error}`; // TODO: localize
}
setTimeout(() => { // TODO: replace this toast
errorMessage.value = null;
}, 5000);
} }
} catch (error: unknown) {
if (error instanceof FetchError) {
errorMessage.value = error.statusText;
} else {
if (error === 'challenge-closed') { // Thrown if the captcha is closed, can be safely ignored
return;
}
errorMessage.value = `Error during registration: ${error}`; // TODO: localize
}
setTimeout(() => { // TODO: replace this toast
errorMessage.value = null;
}, 5000);
} }
} }
</script> </script>
@ -119,8 +117,9 @@ async function registerSubmission() {
</form> </form>
</div> </div>
<vue-hcaptcha <vue-hcaptcha
v-if="$config.public.hCaptchaSitekey"
ref="invisibleHcaptcha" ref="invisibleHcaptcha"
sitekey="cf3fd74e-93ca-47e6-9fa0-5fc439de06d4" :sitekey="$config.public.hCaptchaSitekey"
class="h-captcha" class="h-captcha"
size="invisible" size="invisible"
/> />