fix: resolve unique constraint username duplicate key violation by cleaning stale users on auth
Browse files- users/authentication.py +15 -5
users/authentication.py
CHANGED
|
@@ -154,11 +154,21 @@ class SupabaseAuthentication(authentication.BaseAuthentication):
|
|
| 154 |
|
| 155 |
# 6. Find or Create the User
|
| 156 |
try:
|
| 157 |
-
user
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
print(f"[AUTH DEBUG] ✅ Created new user: {email}")
|
| 163 |
else:
|
| 164 |
print(f"[AUTH DEBUG] ✅ Found existing user: {email}")
|
|
|
|
| 154 |
|
| 155 |
# 6. Find or Create the User
|
| 156 |
try:
|
| 157 |
+
# Check if user already exists with this UUID
|
| 158 |
+
user = User.objects.filter(id=user_id).first()
|
| 159 |
+
if not user:
|
| 160 |
+
# Check if a user with the same email or username exists (stale user)
|
| 161 |
+
stale_user = User.objects.filter(username=email).first() or User.objects.filter(email=email).first()
|
| 162 |
+
if stale_user:
|
| 163 |
+
print(f"[AUTH DEBUG] ⚠️ Stale user found with different UUID (existing: {stale_user.id}, new: {user_id}). Deleting stale user.")
|
| 164 |
+
stale_user.delete()
|
| 165 |
+
|
| 166 |
+
# Create the new user with the new UUID
|
| 167 |
+
user = User.objects.create(
|
| 168 |
+
id=user_id,
|
| 169 |
+
email=email,
|
| 170 |
+
username=email
|
| 171 |
+
)
|
| 172 |
print(f"[AUTH DEBUG] ✅ Created new user: {email}")
|
| 173 |
else:
|
| 174 |
print(f"[AUTH DEBUG] ✅ Found existing user: {email}")
|