David Ko commited on
Commit
64663bd
·
1 Parent(s): 68634f6

Fix login redirect loop by improving login and serve_react functions

Browse files
Files changed (1) hide show
  1. api.py +10 -4
api.py CHANGED
@@ -1153,8 +1153,8 @@ def login():
1153
  if next_page and next_page.startswith('/') and next_page != '/login':
1154
  print(f"Redirecting to: {next_page}")
1155
  return redirect(next_page)
1156
- print("Redirecting to root page")
1157
- return redirect('/')
1158
  else:
1159
  error = 'Invalid username or password'
1160
  print(f"Login failed: {error}")
@@ -1171,10 +1171,16 @@ def logout():
1171
  @login_required
1172
  def serve_react(path):
1173
  """Serve React frontend"""
1174
- print(f"Serving React frontend for path: {path}, user: {current_user.username}")
1175
- if path != "" and os.path.exists(os.path.join(app.static_folder, path)):
 
 
 
 
 
1176
  return send_from_directory(app.static_folder, path)
1177
  else:
 
1178
  return send_from_directory(app.static_folder, 'index.html')
1179
 
1180
  @app.route('/similar-images', methods=['GET'])
 
1153
  if next_page and next_page.startswith('/') and next_page != '/login':
1154
  print(f"Redirecting to: {next_page}")
1155
  return redirect(next_page)
1156
+ print("Redirecting to static index.html")
1157
+ return send_from_directory(app.static_folder, 'index.html')
1158
  else:
1159
  error = 'Invalid username or password'
1160
  print(f"Login failed: {error}")
 
1171
  @login_required
1172
  def serve_react(path):
1173
  """Serve React frontend"""
1174
+ print(f"Serving React frontend for path: {path}, user: {current_user.username if current_user.is_authenticated else 'not authenticated'}")
1175
+ # Handle static files directly
1176
+ if path.startswith('static/'):
1177
+ file_path = path[7:] # Remove 'static/' prefix
1178
+ return send_from_directory(app.static_folder, file_path)
1179
+ # Handle other static files in root of static folder
1180
+ elif path != "" and os.path.exists(os.path.join(app.static_folder, path)):
1181
  return send_from_directory(app.static_folder, path)
1182
  else:
1183
+ # Serve the React app's index.html for all other routes
1184
  return send_from_directory(app.static_folder, 'index.html')
1185
 
1186
  @app.route('/similar-images', methods=['GET'])