rairo commited on
Commit
666b966
·
verified ·
1 Parent(s): e6021ca

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +45 -13
main.py CHANGED
@@ -162,26 +162,58 @@ def ensure_user_profile_exists(uid, email=None):
162
  'suspended': False
163
  }
164
 
165
- # Use transaction to ensure atomic write
166
- def create_profile_transaction(current_data):
167
- if current_data is None:
168
- return new_profile
169
- return current_data # Profile already exists
170
-
171
- result = user_ref.transaction(create_profile_transaction)
172
- if result.committed:
173
- logging.info(f"Successfully created profile for UID: {uid}")
174
- return new_profile
175
- else:
176
- logging.error(f"Failed to create profile for UID: {uid} - transaction not committed")
177
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
 
179
  return user_data
180
 
181
  except Exception as e:
182
  logging.error(f"Error ensuring profile exists for UID {uid}: {e}")
 
183
  return None
184
 
 
185
  # Updated signup function with better error handling
186
  @app.route('/api/auth/signup', methods=['POST'])
187
  def signup():
 
162
  'suspended': False
163
  }
164
 
165
+ try:
166
+ # Use transaction to ensure atomic write
167
+ def create_profile_transaction(current_data):
168
+ if current_data is None:
169
+ return new_profile
170
+ return current_data # Profile already exists
171
+
172
+ # Transaction returns the final data, not a result object
173
+ final_data = user_ref.transaction(create_profile_transaction)
174
+
175
+ # Verify the profile was created by checking if it matches our new profile
176
+ if final_data and final_data.get('email') == email:
177
+ logging.info(f"Successfully created profile for UID: {uid}")
178
+ return final_data
179
+ else:
180
+ logging.error(f"Transaction completed but profile verification failed for UID: {uid}")
181
+ # Fall back to direct set if transaction didn't work as expected
182
+ user_ref.set(new_profile)
183
+
184
+ # Verify the direct set worked
185
+ verification = user_ref.get()
186
+ if verification:
187
+ logging.info(f"Profile created via fallback method for UID: {uid}")
188
+ return verification
189
+ else:
190
+ logging.error(f"Both transaction and direct set failed for UID: {uid}")
191
+ return None
192
+
193
+ except Exception as e:
194
+ logging.error(f"Transaction failed for UID {uid}, attempting direct set: {e}")
195
+ try:
196
+ # Fallback to direct set
197
+ user_ref.set(new_profile)
198
+ verification = user_ref.get()
199
+ if verification:
200
+ logging.info(f"Profile created via fallback for UID: {uid}")
201
+ return verification
202
+ else:
203
+ logging.error(f"Fallback set also failed for UID: {uid}")
204
+ return None
205
+ except Exception as e2:
206
+ logging.error(f"Both transaction and fallback failed for UID {uid}: {e2}")
207
+ return None
208
 
209
  return user_data
210
 
211
  except Exception as e:
212
  logging.error(f"Error ensuring profile exists for UID {uid}: {e}")
213
+ logging.error(traceback.format_exc())
214
  return None
215
 
216
+
217
  # Updated signup function with better error handling
218
  @app.route('/api/auth/signup', methods=['POST'])
219
  def signup():