Skip to content

Conversation

manerlan
Copy link
Member

Part of #29. Backend for user accounts. The frontend (login, registration and profile pages) comes in a separate PR.

What

  • Database: new migration with app_user (id generated by PostgreSQL, unique lowercase username checked by the database, password hash) and user_session (SHA-256 hash of the session token, expiry, ON DELETE CASCADE).
  • Auth module in backend/src/auth/:
    • passwords hashed with scrypt from node:crypto (OWASP parameters, stored with each hash, no new dependency)
    • random 256-bit session tokens; only their hash is stored
    • httpOnly, SameSite=Lax session cookie (kk_session), 30 days
    • username and password rules, and max 10 failed logins per username per 15 minutes
    • the same error for wrong username and wrong password
  • GraphQL: me, register, login, logout. Errors use codes (BAD_USER_INPUT with field/reason, USERNAME_TAKEN, INVALID_CREDENTIALS, TOO_MANY_ATTEMPTS, UNAUTHENTICATED) so the frontend can show Norwegian text.
  • Context: context.ts builds loaders and auth per request. The session is only looked up when a resolver needs the user, so wine queries cost nothing extra. auth.requireUser() is ready for favourites, lists and reviews.
  • Docs: "Users and login" and "Database tests" in docs/api.md, and COOKIE_SECURE / TEST_DATABASE_URL in .env.example.

Why this design

Sessions in the database instead of JWT: logout really ends the session, the token never reaches JavaScript, and the state can be checked with psql. See the table in docs/api.md for choices and alternatives.

How it was tested

  • npm run db:migrate, db:migrate:down and db:migrate again. \d app_user and \d user_session show the expected columns, constraints and indexes.
  • The database rejects an invalid username ('Ola Nordmann' breaks app_user_username_check).
  • npm run typecheck and npm test in backend/.
  • npm test with TEST_DATABASE_URL against a separate test database. The PostgreSQL tests run instead of being skipped.
  • By hand with curl against npm run dev: register sets a cookie, me returns the user with the cookie and null without it, a wrong password gives INVALID_CREDENTIALS, and logout clears the cookie and deletes the session row.
  • psql: the password hash starts with scrypt$32768$8$3$, and the session expires in 30 days and disappears on logout.
  • npm run check: backend TypeScript and tests pass. Frontend Prettier, tests and build fail on my machine, but this PR changes no frontend files (same result on main). To be fixed before the frontend PR.

How to review

The commits are small and build on each other, so reviewing commit by commit is easiest:

  1. migration
  2. password hashing, session tokens and cookie helpers
  3. username and password rules and the login limit
  4. user store and auth service (SQL and logic)
  5. GraphQL API and wiring in server.ts

To try it locally: npm run db:migrate, then the curl commands in "Users and login" in docs/api.md.

Not in this PR

  • Frontend pages (next PR, closes Account system #29).
  • The reviews API still takes an anonymous userId. It should use auth.requireUser() when reviews are tied to users.
  • The cookie is not Secure on the VM, because it serves plain HTTP (COOKIE_SECURE=true turns it on).

AI

Code, tests and docs were drafted with Claude and then applied, run and checked commit by commit by me.

@manerlan manerlan requested a review from a team as a code owner November 12, 2026 14:42
@manerlan manerlan linked an issue Nov 12, 2026 that may be closed by this pull request
@oscarba oscarba merged commit 7451c25 into main Nov 12, 2026
@oscarba oscarba deleted the 29-account-system branch November 12, 2026 14:44
@manerlan manerlan restored the 29-account-system branch November 12, 2026 14:45
@manerlan manerlan deleted the 29-account-system branch November 18, 2026 10:24
Sign in to join this conversation on GitHub.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Account system
2 participants