Update main.py
Browse files
main.py
CHANGED
|
@@ -95,17 +95,28 @@ def normalize_currency_code(raw_code, default_code='USD'):
|
|
| 95 |
# -----------------------------------------------------------------------------
|
| 96 |
# 3. AUTHENTICATION & USER MANAGEMENT ENDPOINTS
|
| 97 |
# -----------------------------------------------------------------------------
|
| 98 |
-
|
| 99 |
@app.route('/api/auth/signup', methods=['POST'])
|
| 100 |
def signup():
|
| 101 |
"""Handles new user sign-up with email/password and creates their Firestore profile."""
|
| 102 |
try:
|
| 103 |
data = request.get_json()
|
| 104 |
email, password, display_name = data.get('email'), data.get('password'), data.get('displayName')
|
|
|
|
| 105 |
|
| 106 |
if not email or not password or not display_name:
|
| 107 |
return jsonify({'error': 'Email, password, and display name are required'}), 400
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
# Step 1: Create the user in Firebase Authentication
|
| 110 |
user = auth.create_user(
|
| 111 |
email=email,
|
|
@@ -119,14 +130,18 @@ def signup():
|
|
| 119 |
'email': email,
|
| 120 |
'displayName': display_name,
|
| 121 |
'isAdmin': False,
|
| 122 |
-
'phone':
|
| 123 |
-
'phoneStatus': 'unsubmitted',
|
| 124 |
'organizationId': None,
|
| 125 |
'createdAt': firestore.SERVER_TIMESTAMP # This is for Firestore
|
| 126 |
}
|
| 127 |
db.collection('users').document(user.uid).set(user_data_for_db)
|
| 128 |
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
# --- THE FIX IS HERE ---
|
| 132 |
# Step 3: Create a SEPARATE dictionary for the JSON response
|
|
@@ -134,7 +149,16 @@ def signup():
|
|
| 134 |
response_data = user_data_for_db.copy()
|
| 135 |
response_data['createdAt'] = datetime.utcnow().isoformat() + "Z" # This is for the client
|
| 136 |
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
except Exception as e:
|
| 140 |
logging.error(f"Signup failed: {e}", exc_info=True) # exc_info=True gives more detail
|
|
|
|
| 95 |
# -----------------------------------------------------------------------------
|
| 96 |
# 3. AUTHENTICATION & USER MANAGEMENT ENDPOINTS
|
| 97 |
# -----------------------------------------------------------------------------
|
|
|
|
| 98 |
@app.route('/api/auth/signup', methods=['POST'])
|
| 99 |
def signup():
|
| 100 |
"""Handles new user sign-up with email/password and creates their Firestore profile."""
|
| 101 |
try:
|
| 102 |
data = request.get_json()
|
| 103 |
email, password, display_name = data.get('email'), data.get('password'), data.get('displayName')
|
| 104 |
+
phone = data.get('phone') # Optional phone number
|
| 105 |
|
| 106 |
if not email or not password or not display_name:
|
| 107 |
return jsonify({'error': 'Email, password, and display name are required'}), 400
|
| 108 |
|
| 109 |
+
# Validate phone number if provided
|
| 110 |
+
if phone:
|
| 111 |
+
phone = phone.strip()
|
| 112 |
+
if not phone:
|
| 113 |
+
phone = None # Treat empty string as None
|
| 114 |
+
else:
|
| 115 |
+
# Check if phone number is already registered
|
| 116 |
+
existing_user_query = db.collection('users').where('phone', '==', phone).limit(1).stream()
|
| 117 |
+
if len(list(existing_user_query)) > 0:
|
| 118 |
+
return jsonify({'error': 'This phone number is already registered to another account.'}), 409
|
| 119 |
+
|
| 120 |
# Step 1: Create the user in Firebase Authentication
|
| 121 |
user = auth.create_user(
|
| 122 |
email=email,
|
|
|
|
| 130 |
'email': email,
|
| 131 |
'displayName': display_name,
|
| 132 |
'isAdmin': False,
|
| 133 |
+
'phone': phone,
|
| 134 |
+
'phoneStatus': 'pending' if phone else 'unsubmitted', # Auto-submit for approval if phone provided
|
| 135 |
'organizationId': None,
|
| 136 |
'createdAt': firestore.SERVER_TIMESTAMP # This is for Firestore
|
| 137 |
}
|
| 138 |
db.collection('users').document(user.uid).set(user_data_for_db)
|
| 139 |
|
| 140 |
+
# Log signup with phone status
|
| 141 |
+
if phone:
|
| 142 |
+
logging.info(f"New user signed up: {user.uid}, Name: {display_name}, Phone: {phone} (submitted for approval)")
|
| 143 |
+
else:
|
| 144 |
+
logging.info(f"New user signed up: {user.uid}, Name: {display_name} (no phone number)")
|
| 145 |
|
| 146 |
# --- THE FIX IS HERE ---
|
| 147 |
# Step 3: Create a SEPARATE dictionary for the JSON response
|
|
|
|
| 149 |
response_data = user_data_for_db.copy()
|
| 150 |
response_data['createdAt'] = datetime.utcnow().isoformat() + "Z" # This is for the client
|
| 151 |
|
| 152 |
+
# Add appropriate success message based on phone submission
|
| 153 |
+
success_message = 'Account created successfully.'
|
| 154 |
+
if phone:
|
| 155 |
+
success_message += ' Your phone number has been submitted for admin approval.'
|
| 156 |
+
|
| 157 |
+
return jsonify({
|
| 158 |
+
'success': True,
|
| 159 |
+
'message': success_message,
|
| 160 |
+
**response_data
|
| 161 |
+
}), 201
|
| 162 |
|
| 163 |
except Exception as e:
|
| 164 |
logging.error(f"Signup failed: {e}", exc_info=True) # exc_info=True gives more detail
|