feat: enhance contact form security and add animated hero
This commit is contained in:
parent
ea18dcdb8e
commit
8497cd819d
19 changed files with 320 additions and 112 deletions
|
|
@ -1,32 +1,52 @@
|
|||
<template>
|
||||
<div class="max-w-xl mx-auto">
|
||||
<form @submit.prevent="sendTextMessage">
|
||||
<!-- Name Input -->
|
||||
<div class="form-control mb-4">
|
||||
<input
|
||||
v-model="userName"
|
||||
type="text"
|
||||
placeholder="Your Name"
|
||||
aria-label="Your Name"
|
||||
pattern="^[\x20-\x7E]*$"
|
||||
class="input input-bordered w-full"
|
||||
:disabled="isMessageSent"
|
||||
required
|
||||
@input="trackActivity('k')"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div
|
||||
class="form-control"
|
||||
style="
|
||||
position: absolute;
|
||||
left: -9999px;
|
||||
opacity: 0;
|
||||
pointer-events: none;
|
||||
"
|
||||
>
|
||||
<input
|
||||
v-model="website"
|
||||
type="text"
|
||||
name="website"
|
||||
placeholder="Website"
|
||||
tabindex="-1"
|
||||
autocomplete="off"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<!-- Message Textarea -->
|
||||
<div class="form-control mb-4">
|
||||
<textarea
|
||||
v-model="userMessage"
|
||||
class="textarea textarea-bordered h-32 w-full"
|
||||
placeholder="Your message..."
|
||||
aria-label="Your message..."
|
||||
aria-label="Your message (only printable ASCII characters allowed)"
|
||||
:disabled="isMessageSent"
|
||||
maxlength="140"
|
||||
pattern="^[\x20-\x7E\n\r]*$"
|
||||
title="Only printable ASCII characters are allowed."
|
||||
required
|
||||
></textarea>
|
||||
@input="trackActivity('k')"
|
||||
/>
|
||||
<div
|
||||
class="text-right text-sm mt-1"
|
||||
:class="{ 'text-error': userMessage.length > 140 }"
|
||||
|
|
@ -35,7 +55,6 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Phone Number and Verification (conditionally enabled) -->
|
||||
<div class="flex flex-col md:flex-row gap-4 mb-4">
|
||||
<div class="form-control w-full md:w-1/2">
|
||||
<div class="join w-full">
|
||||
|
|
@ -59,7 +78,7 @@
|
|||
"
|
||||
@click="sendCode"
|
||||
>
|
||||
<span v-if="isSendingCode" class="loading loading-spinner"></span>
|
||||
<span v-if="isSendingCode" class="loading loading-spinner" />
|
||||
Send Code
|
||||
</button>
|
||||
</div>
|
||||
|
|
@ -80,14 +99,13 @@
|
|||
:disabled="!isCodeSent || isVerified || isVerifying"
|
||||
@click="verifyCode"
|
||||
>
|
||||
<span v-if="isVerifying" class="loading loading-spinner"></span>
|
||||
<span v-if="isVerifying" class="loading loading-spinner" />
|
||||
Verify
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Submit and Status Messages -->
|
||||
<div class="text-center">
|
||||
<div v-if="errorMessage" class="alert alert-error mb-4">
|
||||
<svg
|
||||
|
|
@ -126,7 +144,7 @@
|
|||
class="btn btn-primary w-full"
|
||||
:disabled="!isVerified || isSendingMessage || isMessageSent"
|
||||
>
|
||||
<span v-if="isSendingMessage" class="loading loading-spinner"></span>
|
||||
<span v-if="isSendingMessage" class="loading loading-spinner" />
|
||||
Text Me! (Rob ☺️)
|
||||
</button>
|
||||
</div>
|
||||
|
|
@ -135,15 +153,14 @@
|
|||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, computed } from "vue";
|
||||
import { ref, computed, onMounted } from "vue";
|
||||
|
||||
// --- Form State ---
|
||||
const userName = ref("");
|
||||
const phoneNumber = ref("");
|
||||
const verificationCode = ref("");
|
||||
const userMessage = ref("");
|
||||
const website = ref("");
|
||||
|
||||
// --- UI State ---
|
||||
const isSendingCode = ref(false);
|
||||
const isCodeSent = ref(false);
|
||||
const isVerifying = ref(false);
|
||||
|
|
@ -152,24 +169,24 @@ const isSendingMessage = ref(false);
|
|||
const isMessageSent = ref(false);
|
||||
const errorMessage = ref("");
|
||||
|
||||
// --- Computed Properties ---
|
||||
const formStartTime = ref(0);
|
||||
const hasInteracted = ref(false);
|
||||
const mouseActivity = ref(0);
|
||||
const keyboardActivity = ref(0);
|
||||
|
||||
const isNameAndMessageEntered = computed(() => {
|
||||
const printableAsciiRegex = /^[\x20-\x7E\n\r]*$/;
|
||||
return (
|
||||
userName.value.trim() !== "" &&
|
||||
userMessage.value.trim() !== "" &&
|
||||
userMessage.value.length <= 140 &&
|
||||
printableAsciiRegex.test(userMessage.value)
|
||||
userMessage.value.length <= 140
|
||||
);
|
||||
});
|
||||
|
||||
const isPhoneNumberIncomplete = computed(() => {
|
||||
// Count only the digits in the phone number
|
||||
const digitCount = (phoneNumber.value.match(/\d/g) || []).length;
|
||||
return digitCount < 10;
|
||||
return digitCount < 10 || digitCount > 11;
|
||||
});
|
||||
|
||||
// --- Functions ---
|
||||
const clearError = () => {
|
||||
errorMessage.value = "";
|
||||
};
|
||||
|
|
@ -177,12 +194,14 @@ const clearError = () => {
|
|||
const sendCode = async () => {
|
||||
clearError();
|
||||
if (!phoneNumber.value) {
|
||||
errorMessage.value = "Please enter a valid phone number.";
|
||||
errorMessage.value = "Please enter a valid US phone number.";
|
||||
return;
|
||||
}
|
||||
isSendingCode.value = true;
|
||||
try {
|
||||
const response = await $fetch("/api/send-otp", {
|
||||
const endpoint = atob("L2FwaS9zZW5kLW90cA==");
|
||||
console.log("Using endpoint", endpoint);
|
||||
const response = await $fetch(endpoint, {
|
||||
method: "POST",
|
||||
body: { phoneNumber: phoneNumber.value },
|
||||
});
|
||||
|
|
@ -207,7 +226,9 @@ const verifyCode = async () => {
|
|||
}
|
||||
isVerifying.value = true;
|
||||
try {
|
||||
const response = await $fetch("/api/verify-otp", {
|
||||
const endpoint = atob("L2FwaS92ZXJpZnktb3Rw");
|
||||
console.log("Using endpoint", endpoint);
|
||||
const response = await $fetch(endpoint, {
|
||||
method: "POST",
|
||||
body: {
|
||||
code: verificationCode.value,
|
||||
|
|
@ -216,7 +237,7 @@ const verifyCode = async () => {
|
|||
});
|
||||
if (response.success) {
|
||||
isVerified.value = true;
|
||||
errorMessage.value = ""; // Clear error on success
|
||||
errorMessage.value = "";
|
||||
}
|
||||
} catch (error) {
|
||||
errorMessage.value =
|
||||
|
|
@ -229,6 +250,7 @@ const verifyCode = async () => {
|
|||
|
||||
const sendTextMessage = async () => {
|
||||
clearError();
|
||||
|
||||
if (!isNameAndMessageEntered.value) {
|
||||
errorMessage.value =
|
||||
"Please fill out your name and a valid message before sending.";
|
||||
|
|
@ -237,13 +259,21 @@ const sendTextMessage = async () => {
|
|||
isSendingMessage.value = true;
|
||||
|
||||
try {
|
||||
const response = await $fetch("/api/send-message", {
|
||||
const endpoint = atob("L2FwaS9zZW5kLW1lc3NhZ2U=");
|
||||
console.log("Using endpoint", endpoint);
|
||||
const response = await $fetch(endpoint, {
|
||||
method: "POST",
|
||||
body: {
|
||||
name: userName.value,
|
||||
message: userMessage.value,
|
||||
phoneNumber: phoneNumber.value,
|
||||
code: verificationCode.value,
|
||||
website: website.value,
|
||||
interactionData: {
|
||||
timeSpent: Date.now() - formStartTime.value,
|
||||
mouseActivity: mouseActivity.value,
|
||||
keyboardActivity: keyboardActivity.value,
|
||||
},
|
||||
},
|
||||
});
|
||||
if (response.success) {
|
||||
|
|
@ -259,4 +289,25 @@ const sendTextMessage = async () => {
|
|||
isSendingMessage.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const trackActivity = (type) => {
|
||||
if (type === "m") mouseActivity.value++;
|
||||
if (type === "k") keyboardActivity.value++;
|
||||
hasInteracted.value = true;
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
formStartTime.value = Date.now();
|
||||
|
||||
const events = [
|
||||
["mousemove", () => trackActivity("m")],
|
||||
["click", () => trackActivity("m")],
|
||||
["keydown", () => trackActivity("k")],
|
||||
["keyup", () => trackActivity("k")],
|
||||
];
|
||||
|
||||
events.forEach(([event, handler]) => {
|
||||
document.addEventListener(event, handler, { passive: true });
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue