Skip to content

feat(auth): add email/password auth with JWT tokens#1

Merged
danielvm-git merged 3 commits into
mainfrom
auth-slice
May 26, 2026
Merged

feat(auth): add email/password auth with JWT tokens#1
danielvm-git merged 3 commits into
mainfrom
auth-slice

Conversation

@danielvm-git
Copy link
Copy Markdown
Owner

Summary

Auth component (Slice 4) with email/password registration, login, and JWT-based middleware for protecting API routes.

Changes

  • POST /api/auth/register — bcrypt-hashed password, returns signed JWT
  • POST /api/auth/login — email/password verification, returns signed JWT
  • Middleware() — Bearer token validation via HS256 JWT, sets user info in Go context
  • Auto-creates danielvm table (SQLite), auto-generates 256-bit JWT secret
  • ADR 002 documenting all auth decisions

Details

  • Dependencies: golang.org/x/crypto (bcrypt), github.com/golang-jwt/jwt/v5
  • 22 tests, 83.5% coverage, 0 lint issues
  • Audit-code: ✓ Request-review: ✓ (score 100%)
  • Auth middleware wraps /api/collections/ routes; /api/auth/ is public

Key design decisions

  • HS256 JWT with 24h expiry, auto-generated secret (configurable)
  • Email normalized to lowercase; min 6-char password
  • Generic "invalid email or password" error prevents user enumeration
  • 5s context timeout on all DB ops, 1MB max request body

Auth component (components/auth/):
- User registration (POST /api/auth/register) with bcrypt password hashing
- User login (POST /api/auth/login) returning signed JWT
- Middleware() for protecting routes via Bearer token validation
- Auto-creates users table (email UNIQUE, password_hash, created_at)
- Auto-generates 256-bit JWT secret on first start (configurable)
- Context timeout (5s) on all DB operations
- MaxBytesReader (1MB) on request bodies

Integrates with proxy:
- /api/auth/ endpoints registered as public routes
- /api/collections/ wrapped with auth middleware (JWT required)

Dependencies: golang-jwt/jwt/v5, golang.org/x/crypto/bcrypt
Tests: 13 tests covering register, login, duplicates, validation, middleware

Defensive-code categories: Timeout (5s context deadline), Body limits (1MB)
- Extract decodeBody / writeAuthResponse to eliminate handler duplication
- Replace magic number 1<<20 with maxBodyBytes constant
- Add tests for ConfigSchema, Hooks, Version (coverage 75.8% → 80.0%)
- Add ADR 002 documenting auth decisions (JWT, bcrypt, password policy, token expiry)

Defensive-code categories: Body limits (named constant), Timeout (context deadline)
… context, coverage

Must-fix (3):
- Use res.LastInsertId() instead of separate SELECT with discarded error
- Normalize req.Email to lowercase early in both handlers
- Fix go.mod direct deps marked // indirect

Should-fix (5):
- Extract insertUser + hashPassword helpers to reduce handler length
- Tighten UNIQUE constraint detection ("UNIQUE constraint" substring)
- Handle rand.Read error in secret generation
- Verify registration succeeded before consuming token in tests
- Migrate middleware from HTTP headers to Go context.WithValue

Consider (5):
- Nil guard for Logger and DB in New
- Log JWT verification failures server-side
- Tests: case-insensitive email dedup, empty body, wrong method,
  middleware empty Bearer prefix, context helpers for missing values

Coverage: 75.8% → 83.5%
Score: 38.5% → 100%
@danielvm-git danielvm-git merged commit 35c6096 into main May 26, 2026
@danielvm-git danielvm-git deleted the auth-slice branch June 1, 2026 18:08
github-actions Bot pushed a commit that referenced this pull request Jun 1, 2026
# 1.0.0 (2026-06-01)

### Bug Fixes

* address review findings — rows.Err, routing, YAML leak, repo filter ([45e79c3](45e79c3))
* address review findings — security, authz, race conditions ([2a49228](2a49228))
* **api db:** address all 18 code review findings ([9c693a4](9c693a4))
* **ci:** run smoke test binary with ./ prefix ([83b2de1](83b2de1))
* **deploy:** CI git safe.directory and Caddy http:// catch-all ([d0af465](d0af465))
* **deploy:** enable bigbase systemd unit on VPS setup ([2aa3361](2aa3361))
* **deploy:** resolve relative paths and URL update timing ([9ca144b](9ca144b))
* **deploy:** serve static files via HTTP file server ([575f780](575f780))
* **functions:** address review findings — interrupt timeout, validate update, runtime interface, body limit, injectable runtimes ([a45fa7a](a45fa7a))
* harden kernel, api, auth, forge, git, and storage components ([ef4ea80](ef4ea80))
* **realtime:** fix Hub data race, add event unsubscribe, log emit errors ([f10ed83](f10ed83))
* **ui:** export missing components from index.ts ([994bc53](994bc53))

### Features

* **admin-ui:** port Appwrite design tokens and create shared component system ([da3f118](da3f118))
* **admin:** add Admin UI, SQL Editor, and User Management ([e19e977](e19e977))
* **auth:** add email/password auth with JWT tokens ([#1](#1)) ([35c6096](35c6096))
* **auth:** add Google OAuth social login via embedded relay ([d4970ec](d4970ec))
* **cici:** add CI/CD pipeline component (Slice 9) ([5d2ba35](5d2ba35))
* **cli proxy kernel:** add CLI commands, proxy server, and observability ([fc23a09](fc23a09))
* **db api:** add SQLite database and auto REST CRUD API ([696befb](696befb))
* **deploy:** add deploy component with build detection, process execution, and port allocation ([c8d001a](c8d001a))
* **deploy:** improve deployment pipeline and add admin UI pages ([d056ce1](d056ce1))
* **forge:** add issues, labels, comments, kanban board, and wiki ([e893817](e893817))
* **functions:** add Functions component with CRUD operations ([00ea81c](00ea81c))
* **functions:** add goja JS runtime with console.log capture and timeout ([44a74f5](44a74f5))
* **functions:** wire Functions component into main.go ([9b89585](9b89585))
* **git:** add repository management with bare repo creation ([0481cfc](0481cfc))
* integrate semantic-release and display version in admin footer ([3e6b86c](3e6b86c))
* integrate semantic-release and display version in admin footer ([f042fe2](f042fe2))
* **messaging:** add email, SMS, and push notification endpoints with message log ([fd7cb61](fd7cb61))
* **messaging:** wire messaging component into main.go with auth middleware ([5e48755](5e48755))
* **monitoring:** add /api/monitoring/metrics endpoint with system and request metrics ([dcd0430](dcd0430))
* **monitoring:** add alert CRUD endpoints ([1b356e6](1b356e6))
* **monitoring:** add component shell with health endpoint ([b292c81](b292c81))
* **monitoring:** add log storage and search endpoints ([6d3a6b7](6d3a6b7))
* **monitoring:** add request metrics middleware ([4dfbbc1](4dfbbc1))
* **monitoring:** add system metrics collector ([fc6bd9e](fc6bd9e))
* **monitoring:** wire into main.go and add admin UI page ([0a683cd](0a683cd))
* **proxy:** add commercial landing page and documentation ([c3197e6](c3197e6))
* **proxy:** add commercial landing page with GitHub stars cache ([fea3761](fea3761))
* **proxy:** add documentation page with sidebar navigation ([dcaff68](dcaff68))
* **realtime:** add WebSocket subscriptions, event bus broadcasts, and Hub pattern ([4ab37bf](4ab37bf))
* **storage:** add file upload, download, list, and delete ([78ebe92](78ebe92))
* **ui:** enhance dashboard with charts and fix Google login visibility ([a453256](a453256))

### BREAKING CHANGES

* Deploy now triggers on release.published instead of every push to main.
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 1, 2026

🎉 This PR is included in version 1.0.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant