jdan-basicauthbutton.web.val.run
Readme

basicAuthButton

An authenticated micro-app for tracking the last time I took a medication. Both GET / and POST /press are authenticated using honoBasicAuthMiddleware.

GET /

Views the value of basicAuthButton:last-pressed in blob storage.

image.png

POST /press

Updates the value of basicAuthButton:last-pressed in blob storage with the current timestamp.

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
/** @jsxImportSource npm:hono@3/jsx */
import { honoBasicAuthMiddleware } from "https://esm.town/v/jdan/honoBasicAuthMiddleware";
import { blob } from "https://esm.town/v/std/blob?v=12";
import { Hono } from "npm:hono";
const timeZone = "America/New_York";
const blobStorageKey = "basicAuthButton:last-pressed";
const app = new Hono();
/** Authenticate the URL */
const secretUsername = Deno.env.get("BUTTON_USERNAME");
const secretPassword = Deno.env.get("BUTTON_PASSWORD");
app.use(honoBasicAuthMiddleware(secretUsername, secretPassword));
app.get("/", async (c) => {
const lastPressed = await blob.getJSON(blobStorageKey) || "Never";
return c.html(
<div>
<h1>Last Pressed: {lastPressed}</h1>
<form method="POST" action="/press">
<input type="submit" />
</form>
</div>,
);
});
app.post("/press", async (c) => {
const lastPressed = new Date().toLocaleString("en-US", { timeZone });
await blob.setJSON(blobStorageKey, lastPressed);
return c.redirect("/");
});
export default app.fetch;
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
v23
May 25, 2024