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

@@ -3,7 +3,7 @@ import nunjucks from "nunjucks";
import { contentTypes } from "./content-types";
import { executionContext } from "./execution-context";
import { httpCodes } from "./http-codes";
import type { Result } from "./types";
import type { Result, RedirectResult } from "./types";
// FIXME: Handle the error here
const loadFile = async (path: string): Promise<string> => {
@@ -13,12 +13,14 @@ const loadFile = async (path: string): Promise<string> => {
return data;
};
const render = async (path: string, ctx: object): Promise<string> => {
const render = async (path: string, ctx?: object): Promise<string> => {
const fullPath = `${executionContext.diachron_root}/templates/${path}.html.njk`;
const template = await loadFile(fullPath);
const retval = nunjucks.renderString(template, ctx);
const c = ctx===undefined ? {} : ctx;
const retval = nunjucks.renderString(template, c);
return retval;
};
@@ -33,4 +35,13 @@ const html = (payload: string): Result => {
return retval;
};
export { render, html };
const redirect = (location: string): RedirectResult => {
return {
code: httpCodes.redirection.SeeOther,
contentType: contentTypes.text.plain,
result: "",
redirect: location,
};
};
export { render, html, redirect };