feat(auth): user accounts in the backend (register, login, logout, me) #53
+1,407
−17
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Part of #29. Backend for user accounts. The frontend (login, registration and profile pages) comes in a separate PR.
What
app_user(id generated by PostgreSQL, unique lowercase username checked by the database, password hash) anduser_session(SHA-256 hash of the session token, expiry,ON DELETE CASCADE).backend/src/auth/:node:crypto(OWASP parameters, stored with each hash, no new dependency)httpOnly,SameSite=Laxsession cookie (kk_session), 30 daysme,register,login,logout. Errors use codes (BAD_USER_INPUTwithfield/reason,USERNAME_TAKEN,INVALID_CREDENTIALS,TOO_MANY_ATTEMPTS,UNAUTHENTICATED) so the frontend can show Norwegian text.context.tsbuilds 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/api.md, andCOOKIE_SECURE/TEST_DATABASE_URLin.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 indocs/api.mdfor choices and alternatives.How it was tested
npm run db:migrate,db:migrate:downanddb:migrateagain.\d app_userand\d user_sessionshow the expected columns, constraints and indexes.'Ola Nordmann'breaksapp_user_username_check).npm run typecheckandnpm testinbackend/.npm testwithTEST_DATABASE_URLagainst a separate test database. The PostgreSQL tests run instead of being skipped.curlagainstnpm run dev: register sets a cookie,mereturns the user with the cookie andnullwithout it, a wrong password givesINVALID_CREDENTIALS, and logout clears the cookie and deletes the session row.psql: the password hash starts withscrypt$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 onmain). 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:
server.tsTo try it locally:
npm run db:migrate, then thecurlcommands in "Users and login" indocs/api.md.Not in this PR
userId. It should useauth.requireUser()when reviews are tied to users.Secureon the VM, because it serves plain HTTP (COOKIE_SECURE=trueturns it on).AI
Code, tests and docs were drafted with Claude and then applied, run and checked commit by commit by me.