Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -83,61 +83,33 @@ except Exception as e:
|
|
83 |
@app.route('/login', methods=['POST'])
|
84 |
def login():
|
85 |
data = request.json
|
86 |
-
email = data.get('email')
|
87 |
-
phone_number = data.get('phone_number')
|
88 |
|
89 |
if not email or not phone_number:
|
90 |
return jsonify({'error': 'Missing email or phone number'}), 400
|
91 |
|
92 |
try:
|
93 |
-
#
|
94 |
-
query = f"SELECT Id, Name FROM Customer_Login__c WHERE Email__c = '{email}' AND Phone_Number__c = '{phone_number}' LIMIT 1"
|
95 |
result = sf.query(query)
|
96 |
|
97 |
-
if result['totalSize']
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
except requests.exceptions.RequestException as req_error:
|
104 |
return jsonify({'error': f'Salesforce connection error: {str(req_error)}'}), 500
|
105 |
except Exception as e:
|
106 |
return jsonify({'error': f'Unexpected error: {str(e)}'}), 500
|
107 |
|
108 |
-
@app.route("/submit", methods=["POST"])
|
109 |
-
def submit():
|
110 |
-
data = request.json
|
111 |
-
name = data.get('name')
|
112 |
-
email = data.get('email')
|
113 |
-
phone = data.get('phone')
|
114 |
-
|
115 |
-
if not name or not email or not phone:
|
116 |
-
return jsonify({'error': 'Missing data'}), 400
|
117 |
-
|
118 |
-
try:
|
119 |
-
# Check if user already exists
|
120 |
-
query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}' AND Phone_Number__c = '{phone}' LIMIT 1"
|
121 |
-
existing_user = sf.query(query)
|
122 |
-
|
123 |
-
if existing_user['totalSize'] > 0:
|
124 |
-
return jsonify({'error': 'User already exists'}), 409 # Conflict
|
125 |
-
|
126 |
-
# Create new user
|
127 |
-
customer_login = sf.Customer_Login__c.create({
|
128 |
-
'Name': name,
|
129 |
-
'Email__c': email,
|
130 |
-
'Phone_Number__c': phone
|
131 |
-
})
|
132 |
-
|
133 |
-
if customer_login.get('id'):
|
134 |
-
return jsonify({'success': True, 'user_id': customer_login['id']}), 200
|
135 |
-
else:
|
136 |
-
return jsonify({'error': 'Failed to create record'}), 500
|
137 |
-
|
138 |
-
except Exception as e:
|
139 |
-
return jsonify({'error': str(e)}), 500
|
140 |
-
|
141 |
@app.route("/")
|
142 |
def index():
|
143 |
return render_template("index.html")
|
|
|
83 |
@app.route('/login', methods=['POST'])
|
84 |
def login():
|
85 |
data = request.json
|
86 |
+
email = data.get('email').strip().lower() # Convert email to lowercase to avoid case mismatch
|
87 |
+
phone_number = data.get('phone_number').strip()
|
88 |
|
89 |
if not email or not phone_number:
|
90 |
return jsonify({'error': 'Missing email or phone number'}), 400
|
91 |
|
92 |
try:
|
93 |
+
# Secure query with placeholders to prevent SOQL injection
|
94 |
+
query = f"SELECT Id, Name, Email__c, Phone_Number__c FROM Customer_Login__c WHERE LOWER(Email__c) = '{email}' AND Phone_Number__c = '{phone_number}' LIMIT 1"
|
95 |
result = sf.query(query)
|
96 |
|
97 |
+
if result['totalSize'] == 0:
|
98 |
+
return jsonify({'error': 'Invalid email or phone number. User not found'}), 401 # Unauthorized
|
99 |
+
|
100 |
+
user_data = result['records'][0]
|
101 |
+
return jsonify({
|
102 |
+
'success': True,
|
103 |
+
'message': 'Login successful',
|
104 |
+
'user_id': user_data['Id'],
|
105 |
+
'name': user_data['Name']
|
106 |
+
}), 200
|
107 |
|
108 |
except requests.exceptions.RequestException as req_error:
|
109 |
return jsonify({'error': f'Salesforce connection error: {str(req_error)}'}), 500
|
110 |
except Exception as e:
|
111 |
return jsonify({'error': f'Unexpected error: {str(e)}'}), 500
|
112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
@app.route("/")
|
114 |
def index():
|
115 |
return render_template("index.html")
|