Monorepo for Aesthetic.Computer
aesthetic.computer
1# ATProto User Creation & Sync Investigation Summary
2
3**Date:** October 14, 2025
4**Status:** 🔴 **CRITICAL ISSUES FOUND**
5
6---
7
8## 🔍 Investigation Overview
9
10Investigated the user creation flow from Auth0 signup → MongoDB → ATProto PDS to understand why ATProto accounts are not being automatically created.
11
12---
13
14## 📋 Current Flow (Expected)
15
161. **User Signs Up** → Auth0
172. **Auth0 Log Stream** → Webhook to `/.netlify/functions/auth0-events` (event type: "ss")
183. **auth0-events.mjs**:
19 - ✅ Creates `verifications` record (count: 0)
20 - ✅ Creates `users` record with generated code
214. **User Verifies Email** → Auth0
225. **Auth0 Log Stream** → Webhook to `/.netlify/functions/auth0-events` (event type: "sv")
236. **auth0-events.mjs**:
24 - ✅ Increments `verifications` count to 1
25 - ✅ Calls `createAtprotoAccount()` to create PDS account
26 - ✅ Stores ATProto credentials in `users` collection
27
28---
29
30## 🐛 Issues Found
31
32### Issue #1: Users Records Not Created ❌
33
34**Affected Users:** All 5 recent signups (100%)
35
36**Evidence:**
37```
38MongoDB Collections Status:
39 Verifications: 5/5 ✅ (100%)
40 Users: 0/5 ❌ (0%)
41 Handles: 3/5 (60%)
42 ATProto: 0/5 ❌ (0%)
43```
44
45**Root Cause:**
46The user code generation in `auth0-events.mjs` is failing silently:
47
48```javascript
49try {
50 await ensureUserCodeIndex(database);
51 const signupDate = new Date(log.data.date);
52 const code = await generateUniqueUserCode(database, signupDate);
53
54 const users = database.db.collection("users");
55 await users.insertOne({
56 _id: aestheticSub,
57 code,
58 when: signupDate
59 });
60
61 shell.log("🎫 Generated user code:", code, "for:", aestheticSub);
62} catch (error) {
63 shell.log("⚠️ Failed to generate user code:", aestheticSub, error);
64 // Don't block signup on code generation failure
65}
66```
67
68The error is logged but swallowed, so signup succeeds but user record is never created.
69
70**Impact:**
71- No user codes generated
72- No ATProto accounts created (dependent on user records)
73- Users can still sign up and use handles, but missing identity data
74
75---
76
77### Issue #2: ATProto Accounts Not Created ❌
78
79**Affected Users:** All 4 email-verified users (100%)
80
81**Root Cause:** Cascading failure from Issue #1
82- `createAtprotoAccount()` requires user record to exist
83- Without user record, no code is available for fallback handle
84- ATProto account creation is never attempted
85
86**Evidence from auth0-events.mjs:**
87```javascript
88// 🦋 Create ATProto account on first verification
89shell.log("🦋 Creating ATProto account for newly verified user...");
90const atprotoResult = await createAtprotoAccount(
91 database,
92 aestheticSub,
93);
94```
95
96This code runs, but likely fails because:
971. No user record with code exists
982. User may not have handle set yet
993. `createAtprotoAccount()` needs either handle or code
100
101---
102
103## ✅ What's Working
104
1051. **Auth0 Log Stream Webhook** - Confirmed working
106 - `AUTH0_LOG_TOKEN` is set in Netlify env
107 - Signup events ("ss") are being received
108 - Verification events ("sv") are being received
109
1102. **Verifications Collection** - Working perfectly
111 - All users have verification records
112 - Counts are accurate (0 for unverified, 1 for verified)
113
1143. **Handles Collection** - Partially working
115 - 3 out of 5 users have handles set
116 - Handle creation via `/handle.mjs` endpoint works
117
118---
119
120## 🔧 Recommended Fixes
121
122### Fix #1: Investigate User Code Generation Failure
123
124**Priority:** 🔴 CRITICAL
125
126**Action Items:**
1271. Check Netlify function logs for user code generation errors
1282. Review `generateUniqueUserCode()` implementation
1293. Check if MongoDB index creation is failing
1304. Verify database permissions for inserting into `users` collection
131
132**Command to check logs:**
133```bash
134netlify functions:log auth0-events
135```
136
137### Fix #2: Backfill Missing User Records
138
139**Priority:** 🟡 HIGH
140
141**Action:**
142```bash
143cd /workspaces/aesthetic-computer/at
144node scripts/test-user-creation-flow.mjs 5 --fix
145```
146
147This will:
148- Create missing `users` records with generated codes
149- Create ATProto accounts for email-verified users
150- Sync all data to PDS
151
152**Affected Users:**
153- `auth0|68ee503033f18c5d54238469` - Needs user record (email not verified)
154- `auth0|68ee500f0729405b6c0dfa18` - Needs user record + ATProto account
155- `auth0|68ee4fe6041a48c2971c7fb1` - Needs user record + ATProto account
156- `auth0|68ede47abdf0b654d1475476` - Needs user record + ATProto account
157- `auth0|68ebc6c78aa676dc64c40609` - Needs user record + ATProto account
158
159### Fix #3: Improve Error Handling
160
161**Priority:** 🟢 MEDIUM
162
163Update `auth0-events.mjs` to:
1641. Log full error details (not just message)
1652. Send alerts for user creation failures
1663. Retry user code generation
1674. Create user record with temporary/fallback code if generation fails
168
169---
170
171## 📊 Detailed Audit Results
172
173### Recent Users Status (Last 5 Signups)
174
175#### User 1: auth0|68ee503033f18c5d54238469
176- Email: bjarke.hee@gmail.com
177- Email Verified: ❌
178- Created: 6.1 hours ago
179- Verifications: ✅ (count=0)
180- Users Record: ❌
181- Handle: None
182- ATProto: ❌
183
184#### User 2: auth0|68ee500f0729405b6c0dfa18
185- Email: csx0909@gmail.com
186- Email Verified: ✅
187- Created: 6.1 hours ago
188- Verifications: ✅ (count=1)
189- Users Record: ❌
190- Handle: @csx
191- ATProto: ❌
192
193#### User 3: auth0|68ee4fe6041a48c2971c7fb1
194- Email: violinoletsgo@gmail.com
195- Email Verified: ✅
196- Created: 6.1 hours ago
197- Verifications: ✅ (count=1)
198- Users Record: ❌
199- Handle: None
200- ATProto: ❌
201
202#### User 4: auth0|68ede47abdf0b654d1475476
203- Email: ligands-bays1k@icloud.com
204- Email Verified: ✅
205- Created: 13.7 hours ago
206- Verifications: ✅ (count=1)
207- Users Record: ❌
208- Handle: @zhuxin
209- ATProto: ❌
210
211#### User 5: auth0|68ebc6c78aa676dc64c40609
212- Email: christianbluhme@yahoo.dk
213- Email Verified: ✅
214- Created: 52.3 hours ago (2.2 days)
215- Verifications: ✅ (count=1)
216- Users Record: ❌
217- Handle: @43
218- ATProto: ❌
219
220---
221
222## 📁 Files Involved
223
224### Netlify Functions
225- `/system/netlify/functions/auth0-events.mjs` - Webhook handler (ISSUE HERE)
226- `/system/netlify/functions/handle.mjs` - Handle management
227
228### Backend
229- `/system/backend/at.mjs` - ATProto account creation
230- `/system/backend/authorization.mjs` - User lookup functions
231- `/system/backend/database.mjs` - MongoDB connection
232- `/system/public/aesthetic.computer/lib/user-code.mjs` - Code generation
233
234### Audit Scripts (Created)
235- `/at/scripts/audit-user-creation-sync.mjs` - Full audit tool
236- `/at/scripts/test-user-creation-flow.mjs` - Fix/backfill tool
237- `/at/scripts/check-auth0-webhook-config.mjs` - Config checker
238
239---
240
241## 🎯 Next Steps
242
2431. **Immediate:** Check Netlify logs for actual error messages
2442. **Quick Fix:** Run backfill script with `--fix` flag
2453. **Root Cause:** Debug why user code generation is failing
2464. **Long-term:** Add monitoring/alerting for user creation failures
2475. **Testing:** Create test users to verify fix
248
249---
250
251## 📞 Key Questions to Answer
252
2531. ❓ What is the actual error message from user code generation?
2542. ❓ Is it a MongoDB permission issue?
2553. ❓ Is it a unique index collision issue?
2564. ❓ Has this been working in the past, or is it a new deployment issue?
2575. ❓ Are there any rate limits or throttling on MongoDB writes?
258
259---
260
261**Investigation Tools Available:**
262- `audit-user-creation-sync.mjs` - Check sync status
263- `test-user-creation-flow.mjs` - Simulate/fix user creation
264- `check-auth0-webhook-config.mjs` - Verify webhook config