rairo commited on
Commit
1001868
·
verified ·
1 Parent(s): ec80d00

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +23 -4
main.py CHANGED
@@ -229,17 +229,36 @@ def google_signin():
229
  return jsonify({'error': str(e)}), 400
230
 
231
  # --- 4. ProfAI API ENDPOINTS ---
 
 
232
  @app.route('/api/profai/courses', methods=['GET'])
233
  def get_user_courses():
234
  try:
235
- uid = verify_token(request.headers.get('Authorization', '').split(' ')[1])
236
- if not uid: return jsonify({'error': 'Unauthorized'}), 401
 
 
 
 
 
 
 
237
  courses_ref = db.reference('profai_courses')
 
 
238
  user_courses = courses_ref.order_by_child('uid').equal_to(uid).get()
239
- if not user_courses: return jsonify([]), 200
 
 
 
 
 
240
  courses_list = sorted(user_courses.values(), key=lambda x: x.get('createdAt', ''), reverse=True)
241
  return jsonify(courses_list), 200
242
- except Exception as e: return jsonify({'error': str(e)}), 500
 
 
 
243
 
244
  @app.route('/api/profai/start-course', methods=['POST'])
245
  def start_course():
 
229
  return jsonify({'error': str(e)}), 400
230
 
231
  # --- 4. ProfAI API ENDPOINTS ---
232
+ # In main.py, replace the old get_user_courses function with this one.
233
+
234
  @app.route('/api/profai/courses', methods=['GET'])
235
  def get_user_courses():
236
  try:
237
+ auth_header = request.headers.get('Authorization', '')
238
+ if not auth_header.startswith('Bearer '):
239
+ return jsonify({'error': 'Missing or invalid Authorization header'}), 401
240
+
241
+ token = auth_header.split(' ')[1]
242
+ uid = verify_token(token)
243
+ if not uid:
244
+ return jsonify({'error': 'Unauthorized or invalid token'}), 401
245
+
246
  courses_ref = db.reference('profai_courses')
247
+
248
+ # This query is safe even if the path or user courses don't exist.
249
  user_courses = courses_ref.order_by_child('uid').equal_to(uid).get()
250
+
251
+ # This correctly handles cases where the user has no courses.
252
+ if not user_courses:
253
+ return jsonify([]), 200
254
+
255
+ # Convert dict to a list sorted by creation date
256
  courses_list = sorted(user_courses.values(), key=lambda x: x.get('createdAt', ''), reverse=True)
257
  return jsonify(courses_list), 200
258
+ except Exception as e:
259
+ # This will now only catch truly unexpected server errors
260
+ logger.error(f"Error in get_user_courses: {traceback.format_exc()}")
261
+ return jsonify({'error': str(e)}), 500
262
 
263
  @app.route('/api/profai/start-course', methods=['POST'])
264
  def start_course():