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
// deno-lint-ignore-file no-namespace
/** @jsxImportSource jsr:@hono/hono/jsx **/
import { Context, Hono } from "jsr:@hono/hono";
import { Child } from "jsr:@hono/hono/jsx";
import { sqlite } from "https://esm.town/v/std/sqlite";
import { migrate } from "https://esm.town/v/saolsen/migrations?v=22";
import { eq } from "npm:drizzle-orm";
import { drizzle } from "npm:drizzle-orm/libsql";
import { LibSQLDatabase } from "npm:drizzle-orm/libsql";
import { integer, sqliteTable, text } from "npm:drizzle-orm/sqlite-core";
import { Uuid25 } from "npm:uuid25";
import { uuidv7obj } from "npm:uuidv7";
import { z } from "npm:zod";
import { DrizzleSQLiteAdapter } from "npm:@lucia-auth/adapter-drizzle";
import { Scrypt, Session } from "npm:lucia";
import { Lucia } from "npm:lucia";
namespace schema {
export const KEY = new URL(import.meta.url).pathname.split("/")
.at(-1)!;
export type UserId = string & {
readonly UserId: unique symbol;
};
export const UserId = z
.string()
.startsWith("u_")
.length(27)
.transform((k: string) => k as UserId);
export function userId(): UserId {
return `u_${Uuid25.fromBytes(uuidv7obj().bytes).value}` as UserId;
}
export const users = sqliteTable(`${KEY}_users`, {
id: text("user_id").$type<UserId>().primaryKey(),
username: text("username").unique().notNull(),
passwordHash: text("password_hash").notNull(),
});
export const sessions = sqliteTable(`${KEY}_sessions`, {
id: text("session_id").primaryKey(),
expiresAt: integer("expires_at").notNull(),
userId: text("user_id").$type<UserId>().notNull().references(
() => users.id,
),
});
export const MIGRATIONS = [
`CREATE TABLE ${KEY}_users (
user_id text PRIMARY KEY NOT NULL,
username text NOT NULL,
password_hash text NOT NULL
)`,
`CREATE TABLE ${KEY}_sessions (
session_id text PRIMARY KEY NOT NULL,
expires_at integer NOT NULL,
user_id text NOT NULL,
FOREIGN KEY (user_id) REFERENCES ${KEY}_users(user_id) ON UPDATE no action ON DELETE no action
)`,
`CREATE UNIQUE INDEX ${KEY}_users_username_unique ON ${KEY}_users (username)`,
];
export const schema = { users };
export type DB = LibSQLDatabase<typeof schema>;
}
namespace web {
export type ContextVars = {
db: schema.DB;
user_id: schema.UserId | null;
session: Session | null;
};
const Layout = (
props: {
children: Child;
user?: { username: string } | null;
},
) => {
return (
<html>
<head>
<meta charSet="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>Hono + DaisyUI Site</title>
<link
href="https://cdn.jsdelivr.net/npm/daisyui@4.7.3/dist/full.min.css"
rel="stylesheet"
type="text/css"
/>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/htmx.org@1.9.11">
saolsen-lucia_hono.web.val.run
September 9, 2024