Switch to Cap invisible widget, add form drafts to middleware, and improve OTP

validation

Use the Cap client widget in the contact UI with status icons and auto-solve,
replacing the capwidget element. Normalize and tighten phone validation by
splitting
normalizePhone and isValidPhone in the Otp lib and use it in contact action
validation. Replace loose text validation with a character-stripper helper.
Also bump several dependencies and adjust middleware to save and restore form
data for
form actions.
This commit is contained in:
badblocks 2026-01-27 09:49:06 -08:00
parent f7bdfd3cb8
commit 8e35387841
No known key found for this signature in database
7 changed files with 261 additions and 222 deletions

View file

@ -1,6 +1,5 @@
import { defineMiddleware } from "astro:middleware";
import { getActionContext } from "astro:actions";
import { randomUUID } from "node:crypto";
export const onRequest = defineMiddleware(async (context, next) => {
if (context.isPrerendered) return next();
@ -19,6 +18,7 @@ export const onRequest = defineMiddleware(async (context, next) => {
}
if (action?.calledFrom === "form") {
const formData = await context.request.clone().formData();
const actionResult = await action.handler();
context.session?.set(
@ -30,6 +30,15 @@ export const onRequest = defineMiddleware(async (context, next) => {
);
if (actionResult.error) {
const draft = {
action: formData.get("action")?.toString() ?? "",
name: formData.get("name")?.toString() ?? "",
phone: formData.get("phone")?.toString() ?? "",
msg: formData.get("msg")?.toString() ?? "",
};
context.session?.set("contactFormDraft", draft);
const referer = context.request.headers.get("Referer");
if (!referer) {
throw new Error(
@ -39,6 +48,7 @@ export const onRequest = defineMiddleware(async (context, next) => {
return context.redirect(referer);
}
context.session?.delete("contactFormDraft");
return context.redirect(context.originPathname);
}