Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/** @jsxImportSource https://esm.sh/react */
import React, { useState } from "https://esm.sh/react";
import { createRoot } from "https://esm.sh/react-dom/client";
const relayURL = "https://cephalization-smsjournalertextrelay.web.val.run";
function App() {
const [phoneNumber, setPhoneNumber] = useState("");
const [registrationKey, setRegistrationKey] = useState("");
const [message, setMessage] = useState("");
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
try {
const sanitizedPhoneNumber = phoneNumber.replace(/\D/g, "");
const response = await fetch("/submit", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ phoneNumber: sanitizedPhoneNumber, registrationKey }),
});
if (response.ok) {
setMessage("Phone number submitted successfully!");
setPhoneNumber("");
setRegistrationKey("");
} else {
setMessage("Failed to submit phone number.");
}
} catch (error) {
setMessage("An error occurred. Please try again.");
}
};
return (
<div>
<h1>Phone Number Submission</h1>
<form onSubmit={handleSubmit}>
<input
type="tel"
value={phoneNumber}
onChange={(e) => setPhoneNumber(e.target.value)}
placeholder="Enter phone number"
required
/>
<input
value={registrationKey}
onChange={(e) => setRegistrationKey(e.target.value)}
name="registrationKey"
type="password"
placeholder="Enter registration key"
required
/>
<button type="submit">Submit</button>
</form>
{message && <p>{message}</p>}
<p>
<a href={import.meta.url.replace("esm.town", "val.town")}>View source</a>
</p>
</div>
);
}
function client() {
createRoot(document.getElementById("root")).render(<App />);
}
if (typeof document !== "undefined") { client(); }
async function server(request: Request): Promise<Response> {
const sharedSecret = Deno.env.get("smsjournalersecret");
const url = new URL(request.url);
if (url.pathname === "/submit" && request.method === "POST") {
const { phoneNumber, registrationKey } = await request.json();
// submit to relayUrl/register as a post with phone in json body
// use shared secret as bearer auth
const response = await fetch(relayURL + "/register", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${sharedSecret}`,
},
body: JSON.stringify({ phone: phoneNumber, registrationKey }),
});
if (!response.ok) {
return new Response(JSON.stringify({ success: false }), {
headers: { "Content-Type": "application/json" },
});
}
return new Response(JSON.stringify({ success: true }), {
headers: { "Content-Type": "application/json" },
});
}
return new Response(
`
<html>
<head>
<title>Phone Number Submission</title>
cephalization-smsjournalerregistration.web.val.run
August 24, 2024