Quick Start - Better Auth & NATS Sync

🚀 Get Started in 5 Minutes

The Go backend relies on Better Auth (via the Bun monolith) for identity and NATS JetStream for data synchronization.

1. Start Infrastructure

Ensure you have the following running:
  • PostgreSQL: brew services start postgresql
  • Redis: brew services start redis
  • NATS: nats-server -js (JetStream enabled)

2. Configure Go Backend

Update your backend/go/.env file:
DATABASE_URL=postgres://localhost:5432/gremlin
REDIS_URL=redis://localhost:6379
NATS_URL=nats://localhost:4222
BETTER_AUTH_SECRET=your_32_char_shared_secret
BETTER_AUTH_WEBHOOK_SECRET=your_webhook_signing_secret

3. Run Migrations

Run GORM migrations to set up the PostgreSQL schema:
cd backend/go
go run cmd/server/main.go --migrate

4. Start the Server

Start the Go backend with hot reload (using air):
air

5. Start Bun Monolith (Better Auth Provider)

In a separate terminal, start the Bun monolith which acts as the identity provider:
cd apps/monolith
bun run dev

6. Test Synchronization

  1. Register a User: Sign up a new user via the Bun monolith API or frontend.
  2. Watch NATS: The Bun monolith will publish a user.created event to user.sync.
  3. Check Go Logs: You should see:
    INFO  NATS consumer received message  subject=user.sync
    INFO  User synchronized successfully  auth_id=user_123
    
  4. Verify PostgreSQL:
    SELECT * FROM users;
    

✅ Success Indicators

You’ll know it’s working when:
  • Go backend logs show successful connection to Postgres, Redis, and NATS.
  • New users created in Better Auth automatically appear in the Go users table.
  • Sending a valid JWT to Go endpoints returns 200 OK.

📖 Full Documentation

  • Architecture: docs/backend/go/architecture/authentication.md
  • Troubleshooting: docs/backend/go/guides/troubleshooting.md

🆘 Quick Troubleshooting

“Invalid token” → Check if BETTER_AUTH_SECRET is identical in both Go and Bun. “NATS Connection Refused” → Ensure nats-server -js is running. “User not found” in Go → Check if the NATS consumer go-user-sync is active: nats consumer info user.sync go-user-sync.