Add basic login screen with form-based authentication

Adds /login route with HTML template that handles GET (show form) and
POST (authenticate). On successful login, sets session cookie and
redirects to /. Also adds framework support for redirects and cookies
in route handlers.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-11 10:07:02 -06:00
parent 7cecf5326d
commit 1c1eeddcbe
7 changed files with 188 additions and 9 deletions

View File

@@ -15,6 +15,7 @@ import {
AuthorizationDenied,
type Call,
type InternalHandler,
isRedirect,
type Method,
massageMethod,
methodParser,
@@ -25,8 +26,9 @@ import {
const app = express();
// Parse JSON request bodies
// Parse request bodies
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
services.logging.log({ source: "logging", text: ["1"] });
const processedRoutes: { [K in Method]: ProcessedRoute[] } = {
@@ -111,8 +113,10 @@ async function handler(
const method = await methodParser.parseAsync(req.method);
const byMethod = processedRoutes[method];
console.log("DEBUG: req.path =", JSON.stringify(req.path), "method =", method);
for (const [_idx, pr] of byMethod.entries()) {
const match = pr.matcher(req.url);
const match = pr.matcher(req.path);
console.log("DEBUG: trying pattern, match result =", match);
if (match) {
console.log("match", match);
const resp = await pr.handler(req);
@@ -124,7 +128,7 @@ async function handler(
const retval: Result = {
code: httpCodes.clientErrors.NotFound,
contentType: contentTypes.text.plain,
result: "not found",
result: "not found!",
};
return retval;
@@ -138,7 +142,18 @@ app.use(async (req: ExpressRequest, res: ExpressResponse) => {
console.log(result);
res.status(code).send(result);
// Set any cookies from the result
if (result0.cookies) {
for (const cookie of result0.cookies) {
res.cookie(cookie.name, cookie.value, cookie.options ?? {});
}
}
if (isRedirect(result0)) {
res.redirect(code, result0.redirect);
} else {
res.status(code).send(result);
}
});
process.title = `diachron:${cli.listen.port}`;