Public
Online chatroom with username/password login
authchatreactsqlite
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
A real-time-feeling chatroom with its own username/password accounts. Sign up, log in, and talk to everyone else in the room — messages poll in every couple of seconds, so it works with multiple people at once.
- Username/password accounts — register and log in from the same page
- Password hashing — PBKDF2-SHA256 with a per-user salt, 120k iterations
- Session cookies — a random 256-bit token in an
HttpOnly,Secure,SameSite=Laxcookie, valid for 30 days - Shared message history — the last 200 messages, persisted in SQLite
- Live updates — the client polls for new messages every 2 seconds
- Input validation — 3–20 char usernames, 8+ char passwords, 1000-char message limit, all enforced server-side
- Responsive UI — Twind/Tailwind styling, your own messages right-aligned
Rendering mermaid diagram...
POST /api/registervalidates the input, hashes the password with PBKDF2 and a random salt, and inserts the user. Duplicate usernames are rejected case-insensitively by aUNIQUE COLLATE NOCASEconstraint.- A session row is created with a random 256-bit token and an expiry 30 days
out. The token goes into the response as an
HttpOnlycookie — client JavaScript can never read it. - Every API route reads the cookie, joins
sessionstousers, and rejects expired tokens (which are deleted on sight). POST /api/logoutdeletes the session row and clears the cookie, so the token is worthless even if it was captured.
Passwords are never stored or logged in plaintext; the stored form is
pbkdf2$<iterations>$<salt>$<hash> and verification uses a constant-time
comparison.
index.ts ← Hono server: HTML shell, auth API, chat API
server/
db.ts ← SQLite schema (lazy, idempotent init)
auth.ts ← hashing, sessions, current-user lookup
frontend/
root.tsx ← HTML shell (stamps immutable asset URLs)
index.tsx ← React entrypoint
api.ts ← typed fetch wrappers for the API
types.ts ← shared request/response types
components/
App.tsx ← restores session, routes to auth or chat
AuthForm.tsx ← login / register form
ChatRoom.tsx ← header, polling loop, composer
MessageList.tsx ← message bubbles, auto-scroll
| Method | Route | Purpose |
|---|---|---|
GET | /api/me | Current user, or null |
POST | /api/register | Create an account and start a session |
POST | /api/login | Authenticate and start a session |
POST | /api/logout | Destroy the session |
GET | /api/messages?after=<id> | Last 200 messages, or those after <id> |
POST | /api/messages | Post a message (requires a session) |
- Polling every 2s is simple and reliable; Val Town doesn't accept incoming WebSocket connections, but server-sent events or long polling would cut latency and request volume.
- Messages are shared across all users — there are no rooms yet. Adding a
room_idcolumn and a room picker is the natural next step. - Passwords can't currently be changed, and there's no rate limiting on login attempts. Both are worth adding before opening the room to the public.
No environment variables are required — SQLite and the session tokens are all handled inside the val.