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:
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user