Skip to content

Commit

Permalink
Created urgent contact form
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminEHowe committed Jun 21, 2024
1 parent b3a0340 commit 6cdcfd7
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 6 deletions.
65 changes: 65 additions & 0 deletions contact-urgent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
layout: default
title: Urgent Contact
---

If you have an access token then you can send me an urgent message using this form:

<form id="contact-form" method="post" action="/api/contact-urgent">
<fieldset style="display:none">
<label for="name">Leave blank if you're human:</label>
<input type="text" name="name" id="name" placeholder="Leave blank if you're human">
</fieldset>
<fieldset style="margin-bottom:1em">
<label for="email" style="display:inline-block; margin-bottom:0.5em">Email address:</label>
<input type="email" name="email" id="email" placeholder="Email address" style="box-sizing:border-box; width:100%; max-width:20em" required>
</fieldset>
<fieldset style="margin-bottom:0.5em">
<label for="message">Message:</label>
</fieldset>
<fieldset style="margin-bottom:1em">
<textarea name="message" id="message" placeholder="Message" style="box-sizing:border-box; width:100%; max-width:60em; height:15em" required></textarea>
</fieldset>
<fieldset style="margin-bottom:1em">
<label for="token" style="display:inline-block; margin-bottom:0.5em">Token:</label>
<input type="text" name="token" id="token" placeholder="token" style="box-sizing:border-box; width:100%; max-width:20em" required>
</fieldset>
<button type="submit" style="margin-bottom:1em">Send</button>
</form>

<script>
document.getElementById("contact-form").addEventListener("submit", event => {
event.preventDefault();
const formData = new FormData(event.target);
const token = formData.get("token");
fetch("/api/token-verify", {
method: "POST",
headers: {
"accept": "application/json",
"content-type": "application/json"
},
body: JSON.stringify({ token })
})
.then(res => res.json())
.then(res => {
const element = document.getElementById("token-verify-output");
if (!res.ok) {
alert(`Token ${token} does not appear to be valid.`)
} else {
fetch("/api/contact-urgent", {
method: "POST",
headers: {
"accept": "application/json",
"content-type": "application/json",
},
body: JSON.stringify(Object.fromEntries(formData.entries()))
})
.then(res => res.json())
.then(res => {
alert("Your message has been sent successfully.");
event.target.reset();
})
}
});
});
</script>
7 changes: 5 additions & 2 deletions functions-src/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ async function handleForm({
formId,
honeypotField,
}) {
const headers = Object.fromEntries(context.request.headers.entries());
const usingJson = (headers.content-type === "application/json");
const submissionId = ulid();
const submittedTs = new Date().toISOString();
const fields = Object.fromEntries((await context.request.formData()).entries());
const fields = usingJson ?
await context.request.json() :
Object.fromEntries((await context.request.formData()).entries());
const replyEmail = fields.email;
const spamReasons = [];
const cf = context.request.cf;
const headers = Object.fromEntries(context.request.headers.entries());

if (typeof honeypotField === "string") {
if (fields[honeypotField] !== "") {
Expand Down
28 changes: 28 additions & 0 deletions functions/api/contact-urgent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { handleForm } from "../../functions-src/forms.js"
import { verifySecureToken } from "../../functions-src/secure-token.js"

export async function onRequest(context) {
if (context.request.method !== "POST") {
return new Response("Invalid request method", { status: 405 });
}

const headers = Object.fromEntries(context.request.headers.entries());
const url = new URL(context.request.url)
const usingJson = (headers.content-type === "application/json");

// TODO: verify token

const success = await handleForm({
context,
formId: "URGENT-CONTACT",
honeypotField: "name",
});

if (!success && !usingJson) {
return new Response("Oops! Something went wrong. Please try submitting the form again.", { status: 500 });
}

return usingJson ?
Response. Response.json({ success }) :
Response.redirect(`https://${url.hostname}/contact-success`, 303);
}
8 changes: 4 additions & 4 deletions secure/token-generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ Use this form to generate a token for the urgent contact form.
fetch('/secure/api/token-generate', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
'accept': 'application/json',
'content-type': 'application/json'
},
body: JSON.stringify({ name, expiry })
})
Expand All @@ -55,8 +55,8 @@ Use this form to generate a token for the urgent contact form.
fetch('/api/token-verify', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
'accept': 'application/json',
'content-type': 'application/json'
},
body: JSON.stringify({ token })
})
Expand Down

0 comments on commit 6cdcfd7

Please sign in to comment.