Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/** @jsxImportSource npm:hono/jsx **/
import { createDate, isWithinExpirationDate, TimeSpan } from "npm:oslo";
import codeOnValTown from "https://esm.town/v/andreterron/codeOnValTown?v=50";
import { ValTownAdapter } from "https://esm.town/v/pomdtr/lucia_adapter";
import { createUser, getUser, verifyPassword } from "https://esm.town/v/pomdtr/lucia_sqlite";
import { Hono } from "npm:hono";
import { getCookie } from "npm:hono/cookie";
import { HTTPException } from "npm:hono/http-exception";
import { jsxRenderer } from "npm:hono/jsx-renderer";
import { Lucia, Session, User, verifyRequestOrigin } from "npm:lucia@3.0.1";
const userTable = "user";
const sessionTable = "session";
const adapter = new ValTownAdapter({
user: "user",
session: "session",
});
export const lucia = new Lucia(adapter, {
getUserAttributes: (attributes) => {
return {
username: attributes.username,
};
},
});
declare module "npm:lucia" {
interface Register {
Lucia: typeof lucia;
DatabaseUserAttributes: DatabaseUserAttributes;
}
}
interface DatabaseUserAttributes {
username: string;
}
const router = new Hono<{
Variables: {
user: User | null;
session: Session | null;
};
}>();
router.use("*", async (c, next) => {
const sessionId = getCookie(c, lucia.sessionCookieName) ?? null;
if (!sessionId) {
c.set("user", null);
c.set("session", null);
return next();
}
const { session, user } = await lucia.validateSession(sessionId);
if (session && session.fresh) {
// use `header()` instead of `setCookie()` to avoid TS errors
c.header("Set-Cookie", lucia.createSessionCookie(session.id).serialize(), {
append: true,
});
}
if (!session) {
c.header("Set-Cookie", lucia.createBlankSessionCookie().serialize(), {
append: true,
});
}
c.set("user", user);
c.set("session", session);
return next();
});
router.use(jsxRenderer(({ children }) => (
<html lang="en">
<head>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css"
/>
</head>
<body>
<header class="container">
<nav>
<ul>
<li>
<a href="/" class="contrast">
<strong>Lucia Demo</strong>
</a>
</li>
</ul>
</nav>
</header>
{children}
</body>
</html>
)));
router.get("/", c => {
const user = c.get("user");
if (!user) {
return c.render(
v117 was merged from the PR "Add better attributes to form inputs" by neverstew
pomdtr-lucia_demo.web.val.run
March 6, 2024