Event-Driven Integration - Testing Guide
Testing user synchronization via NATS events published by Better Auth. Important: Better Auth publishes events to NATS (message queue), not webhooks. This guide covers testing NATS event consumption via theuser_sync_handler.go.
Prerequisites
1. NATS Server Running
Better Auth publishes events to a NATS server. Ensure NATS is running:2. Backend Connected to NATS
Verify your.env has correct NATS connection:
3. Install NATS CLI Tools
Testing Options
Option 1: End-to-End User Registration (Recommended)
Triggers a realuser.created event from Better Auth to NATS
Step 1: Start Backend
http://localhost:8080 and connected to NATS.
Step 2: Monitor NATS Events
In a new terminal, subscribe to NATS user events:Step 3: Trigger User Creation
Go to your frontend and sign up:- Navigate to
http://localhost:4321(Astro) orhttp://localhost:3000(SolidStart) - Click “Sign Up”
- Register with email/password or OAuth (Google, GitHub)
- Check NATS subscriber terminal for incoming event
Step 4: Verify Event Received
You should see a message like:Step 5: Verify Backend Processed Event
Check backend logs:Option 2: Publish Manual Test Event to NATS
Simulates a Better Auth event without needing user signupStep 1: Start Backend
Step 2: Subscribe to Events
In terminal 1:Step 3: Publish Test Event
In terminal 2, publish a testuser.created event:
Step 4: Verify Reception
Check terminal 1 (subscriber) - you should see the event published. Check backend logs - verifyuser_sync_handler.go processed it.
Check PostgreSQL:
Option 3: Monitor Live Event Stream
Watch all Better Auth events in real-time- Signing up new users in frontend
- Logging in (may trigger
user.updated) - Updating user profile
Better Auth Event Format
Events published by Better Auth follow this structure:user_sync_handler.go consumes these events and syncs user data to PostgreSQL users table.
Verification Checklist
After testing, verify:- NATS CLI
nats sub "user.>"receives events - Backend logs show
user_sync_handlerprocessing events (no errors) - User created/updated in PostgreSQL
userstable with correctauth_id - User
email,name,rolesmatch Better Auth data - User
organization_idset correctly (from event data or default) - Backend stays connected to NATS (no “connection lost” errors)
Troubleshooting
NATS server not running
Error:Error: Could not connect to NATS server
Fix:
Backend not connected to NATS
Error: Backend logs show “Failed to connect to NATS” or no events being processed Fix:-
Check
.envhas correct NATS_URL: -
Verify NATS server is running:
-
Restart backend after .env changes:
Event not received in subscriber
Error:nats sub "user.>" is running but no events appear
Checks:
- Is backend connected to NATS? (check backend logs)
- Is an event being triggered? (sign up user or publish manually)
- Is NATS working? Test with manual publish:
User not synced to PostgreSQL
Error: Event received butusers table unchanged
Cause: user_sync_handler.go failed to process event
Fix:
- Check backend logs for error message
- Verify PostgreSQL connection:
- Check foreign key constraints:
Email already exists error
Expected behavior: If user already exists,user_sync_handler should update, not error.
If it errors:
- Check
FindByEmaillogic in handler - Verify PostgreSQL
userstable structure - Check backend logs for specific error
Duplicate events received
Expected: Events may be published multiple times (at-least-once delivery). Handler should be idempotent. Verify: Running handler twice with sameuserId should not create duplicates.
Event Types & Handling
user.created
- Triggered: New user signs up (email/password or OAuth)
- Action: Create new record in PostgreSQL
userstable - Handler:
user_sync_handler.goCreateUser logic
user.updated
- Triggered: User updates profile (name, email, roles)
- Action: Update existing record in PostgreSQL
userstable - Handler:
user_sync_handler.goUpdateUser logic
user.deleted
- Triggered: User account deleted (if implemented)
- Action: Mark user as inactive or delete record
- Handler: Currently handled by DeleteUser logic (if present)
Future: Multi-Tenant Support
When ready to support multiple organizations:-
Handle
organizationIdin events:- Currently set to
"org_default" - Will be dynamic when users can create/join orgs
- Currently set to
-
Update event handler (
user_sync_handler.go): -
Add organization management events:
organization.created- New org createdorganization.updated- Org info changedorganizationMembership.created- User joins orgorganizationMembership.deleted- User leaves org
Next Steps
- ✅ Verify NATS server is running
- ✅ Start backend and check NATS connection
- ✅ Test with end-to-end user signup
- ✅ Monitor events in NATS subscriber
- 📝 Verify users synced to PostgreSQL
- 📝 Review
user_sync_handler.goerror handling - 📝 Set up monitoring alerts for NATS connection failures
Support
If you encounter issues:- Review backend logs for NATS handler errors
- Verify NATS server is running and accessible
- Check
.envhas correctNATS_URL - Verify PostgreSQL
userstable exists and has correct schema - Run
nats pingto test NATS connectivity - Check
user_sync_handler.goimplementation for custom logic