logmein
Viewing readonly version: 284View 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 logMeInMiddleware(
importMetaUrl: string,
handle: Handler,
): Promise<Handler> {
const projectInfo = extractProjectValInfo(importMetaUrl);
const db = new DB(projectInfo.projectName);
await db.migrate();
return async (r: Request) => {
const url = new URL(r.url);
if (url.pathname === LOGOUT_URL) {
await db.dropAllSessions();
return new Response(null, { status: 302, headers: { location: "/" } });
}
const sessionKey = getSessionKey(r.headers);
let [activeSession, config] = await Promise.all([sessionKey && db.validateSession(sessionKey), db.getConfig()]);
if (activeSession === true) {
if (url.pathname === RESET_URL) {
await db.deleteConfig();
return new Response(null, { status: 302, headers: { location: "/" } });
}
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" };