feat: add contact form with SMS OTP verification

This commit is contained in:
badblocks 2025-07-17 19:34:29 -07:00
parent 91b162fb44
commit 3874443c34
No known key found for this signature in database
14 changed files with 729 additions and 54 deletions

45
server/lib/http-client.js Normal file
View file

@ -0,0 +1,45 @@
/**
* A simple HTTP client wrapper around the global fetch function.
* This is used by the android-sms-gateway client.
*/
export const httpFetchClient = {
get: async (url, headers) => {
const response = await fetch(url, {
method: "GET",
headers,
});
if (!response.ok) {
const errorBody = await response.text();
console.error("Gateway GET error:", errorBody);
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
},
post: async (url, body, headers) => {
const response = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json", ...headers },
body: JSON.stringify(body),
});
if (!response.ok) {
const errorBody = await response.text();
console.error("Gateway POST error:", errorBody);
throw new Error(
`HTTP error! status: ${response.status}, body: ${errorBody}`,
);
}
return response.json();
},
delete: async (url, headers) => {
const response = await fetch(url, {
method: "DELETE",
headers,
});
if (!response.ok) {
const errorBody = await response.text();
console.error("Gateway DELETE error:", errorBody);
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
},
};

42
server/lib/sms-gateway.js Normal file
View file

@ -0,0 +1,42 @@
import Client from "android-sms-gateway";
import { httpFetchClient } from "./http-client";
/**
* Creates and configures an instance of the android-sms-gateway client.
* It centralizes the client instantiation and ensures that all necessary
* configuration for the gateway is present.
*
* @param {object} config The runtime configuration object from `useRuntimeConfig`.
* It should contain `androidSmsGatewayLogin`, `androidSmsGatewayPassword`,
* and `androidSmsGatewayUrl`.
* @returns {Client} A configured instance of the SMS gateway client.
* @throws {Error} If the required gateway configuration is missing, prompting
* a 500-level error in the calling API endpoint.
*/
export function createSmsGatewayClient(config) {
const {
androidSmsGatewayLogin,
androidSmsGatewayPassword,
androidSmsGatewayUrl,
} = config;
if (
!androidSmsGatewayLogin ||
!androidSmsGatewayPassword ||
!androidSmsGatewayUrl
) {
console.error(
"SMS Gateway service is not configured. Missing required environment variables for the gateway.",
);
// This indicates a critical server misconfiguration. The calling API endpoint
// should handle this and return a generic 500 error to the client.
throw new Error("Server is not configured for sending SMS.");
}
return new Client(
androidSmsGatewayLogin,
androidSmsGatewayPassword,
httpFetchClient,
androidSmsGatewayUrl,
);
}