rairo commited on
Commit
5ef257e
·
verified ·
1 Parent(s): 88960e3

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +75 -0
main.py CHANGED
@@ -54,6 +54,41 @@ except Exception as e:
54
  bucket = storage.bucket()
55
  db_ref = db.reference()
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
  # --- Google GenAI Client Initialization (as per Streamlit app) ---
59
  try:
@@ -182,6 +217,46 @@ def signup():
182
  except Exception as e:
183
  return jsonify({'error': str(e)}), 400
184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  @app.route('/api/user/profile', methods=['GET'])
186
  def get_user_profile():
187
  uid = verify_token(request.headers.get('Authorization'))
 
54
  bucket = storage.bucket()
55
  db_ref = db.reference()
56
 
57
+ # --- Create Dummy Admin Account on Startup ---
58
+ # This block ensures the specified admin user exists with the correct privileges.
59
+ # It's designed to run safely on every application start.
60
+ try:
61
+ ADMIN_EMAIL = "rairorr@gmail.com"
62
+ print(f"Checking for admin user: {ADMIN_EMAIL}")
63
+ try:
64
+ # 1. Check if the user exists in Firebase Auth by email. [4]
65
+ user = auth.get_user_by_email(ADMIN_EMAIL)
66
+ # 2. If user exists, ensure their DB record is correct
67
+ user_ref = db_ref.child(f'users/{user.uid}')
68
+ user_ref.update({
69
+ 'is_admin': True,
70
+ 'credits': 9999
71
+ })
72
+ print(f"Admin user '{ADMIN_EMAIL}' found and privileges verified.")
73
+ except auth.UserNotFoundError:
74
+ # 3. If user does not exist in Auth, create them. [8]
75
+ print(f"Admin user '{ADMIN_EMAIL}' not found, creating a new one.")
76
+ user = auth.create_user(
77
+ email=ADMIN_EMAIL,
78
+ password="neo2025"
79
+ )
80
+ # 4. Create their record in the Realtime Database
81
+ db_ref.child(f'users/{user.uid}').set({
82
+ 'email': ADMIN_EMAIL,
83
+ 'credits': 9999,
84
+ 'is_admin': True,
85
+ 'createdAt': datetime.utcnow().isoformat()
86
+ })
87
+ print(f"Successfully created admin user '{ADMIN_EMAIL}'.")
88
+ except Exception as e:
89
+ # Catch any other exceptions during the process
90
+ print(f"FATAL: An error occurred during admin account setup: {e}")
91
+
92
 
93
  # --- Google GenAI Client Initialization (as per Streamlit app) ---
94
  try:
 
217
  except Exception as e:
218
  return jsonify({'error': str(e)}), 400
219
 
220
+ @app.route('/api/auth/social-signin', methods=['POST'])
221
+ def social_signin():
222
+ """
223
+ Ensures a user record exists in the Realtime Database after a social login
224
+ (like Google Sign-In). The client should call this endpoint immediately after
225
+ a successful Firebase authentication on their side, sending the
226
+ Firebase ID Token. This creates the user's profile in our database if it's
227
+ their first time.
228
+ """
229
+ uid = verify_token(request.headers.get('Authorization'))
230
+ if not uid:
231
+ return jsonify({'error': 'Invalid or expired token'}), 401
232
+
233
+ user_ref = db_ref.child(f'users/{uid}')
234
+ user_data = user_ref.get()
235
+
236
+ if user_data:
237
+ # User already has a profile in our DB, return it.
238
+ return jsonify({'uid': uid, **user_data}), 200
239
+ else:
240
+ # This is a new user (first social login), create their profile.
241
+ try:
242
+ # Get user details from Firebase Auth service. [8]
243
+ firebase_user = auth.get_user(uid)
244
+
245
+ # Create the user profile in our Realtime Database
246
+ new_user_data = {
247
+ 'email': firebase_user.email,
248
+ 'credits': 15, # Standard starting credits
249
+ 'is_admin': False,
250
+ 'createdAt': datetime.utcnow().isoformat()
251
+ }
252
+ user_ref.set(new_user_data)
253
+
254
+ # Return the newly created profile
255
+ return jsonify({'success': True, 'uid': uid, **new_user_data}), 201
256
+ except Exception as e:
257
+ print(f"Error creating profile for new social user {uid}: {e}")
258
+ return jsonify({'error': f'Failed to create user profile: {str(e)}'}), 500
259
+
260
  @app.route('/api/user/profile', methods=['GET'])
261
  def get_user_profile():
262
  uid = verify_token(request.headers.get('Authorization'))