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.

Shadow Chatroom

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.

Open the chatroom →

Features

  • 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=Lax cookie, 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

Architecture

Rendering mermaid diagram...

How auth works

  1. POST /api/register validates the input, hashes the password with PBKDF2 and a random salt, and inserts the user. Duplicate usernames are rejected case-insensitively by a UNIQUE COLLATE NOCASE constraint.
  2. 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 HttpOnly cookie — client JavaScript can never read it.
  3. Every API route reads the cookie, joins sessions to users, and rejects expired tokens (which are deleted on sight).
  4. POST /api/logout deletes 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.

File structure

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

API

MethodRoutePurpose
GET/api/meCurrent user, or null
POST/api/registerCreate an account and start a session
POST/api/loginAuthenticate and start a session
POST/api/logoutDestroy the session
GET/api/messages?after=<id>Last 200 messages, or those after <id>
POST/api/messagesPost a message (requires a session)

Notes and possible next steps

  • 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_id column 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.