logmein
Viewing readonly version: 238View latest version
Script
999
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
/** @jsxImportSource npm:react */
import { extractProjectValInfo } from "https://esm.town/v/maxm/extractProjectValInfo";
import { renderToString } from "npm:react-dom/server";
import { type Config, DB } from "./db";
import { getSessionKey, setSession } from "./sessionCookie";
type Handler = (r: Request) => Promise<Response> | Response;
export async function middleware(
importMetaUrl: string,
handle: Handler,
): Promise<Handler> {
const projectInfo = extractProjectValInfo(importMetaUrl);
const db = new DB(projectInfo.projectName);
await db.migrate();
return async (r: Request) => {
const sessionKey = getSessionKey(r.headers);
let [activeSession, config] = await Promise.all([sessionKey && db.validateSession(sessionKey), db.getConfig()]);
if (activeSession === true) {
return handle(r);
}
return await (new MiddlewareState({ config, db, req: r, projectInfo })).landingPage();
};
}
function htmlTemplateResponse(body: JSX.Element, options?: Partial<ResponseInit>): Response {
const headers = { "content-type": "text/html charset=utf-8" };
return new Response(
renderToString(
<html>
<head>
<title>Log Me In</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" rel="stylesheet" />
<style>{css}</style>
</head>