sunheycho commited on
Commit
662d544
ยท
1 Parent(s): 7a9c716

Fix Hugging Face Spaces login redirect loop

Browse files

- Configure session cookies for HTTPS and iframe embedding (SameSite=None)
- Remove fresh login requirements that cause redirect loops in HF Spaces
- Enable CORS with credentials support
- Add cache control headers for proper redirects
- Update cookie security settings for production environment

Files changed (1) hide show
  1. api.py +18 -16
api.py CHANGED
@@ -75,15 +75,15 @@ app.secret_key = secret_key # ์„ธ์…˜ ์•”ํ˜ธํ™”๋ฅผ ์œ„ํ•œ ๋น„๋ฐ€ ํ‚ค
75
  app.config['CORS_HEADERS'] = 'Content-Type'
76
  # Remember cookie (Flask-Login) โ€” minimize duration to prevent auto re-login
77
  app.config['REMEMBER_COOKIE_DURATION'] = timedelta(seconds=1)
78
- app.config['REMEMBER_COOKIE_SECURE'] = False # Allow HTTP for local dev
79
  app.config['REMEMBER_COOKIE_HTTPONLY'] = True
80
- app.config['REMEMBER_COOKIE_SAMESITE'] = 'Lax'
81
- # Session cookie (Flask-Session) - relaxed for local dev
82
- app.config['SESSION_COOKIE_SECURE'] = False # Allow HTTP for local dev
83
  app.config['SESSION_COOKIE_HTTPONLY'] = True
84
- app.config['SESSION_COOKIE_SAMESITE'] = 'Lax' # More permissive for local dev
85
  app.config['SESSION_COOKIE_PATH'] = '/'
86
- CORS(app) # Enable CORS for all routes
87
 
88
  # ์‹œํฌ๋ฆฟ ํ‚ค ์„ค์ • (์„ธ์…˜ ์•”ํ˜ธํ™”์— ์‚ฌ์šฉ)
89
  app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'vision_llm_agent_secret_key')
@@ -1372,13 +1372,10 @@ LOGIN_TEMPLATE = '''
1372
 
1373
  @app.route('/login', methods=['GET', 'POST'])
1374
  def login():
1375
- # ์ด๋ฏธ ๋กœ๊ทธ์ธ๋œ ์‚ฌ์šฉ์ž๋Š” ๋ฉ”์ธ ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋””๋ ‰์…˜
1376
- if current_user.is_authenticated and login_fresh():
1377
- print(f"User already authenticated and fresh as: {current_user.username}, redirecting to index")
1378
  return redirect('/index.html')
1379
- elif current_user.is_authenticated and not login_fresh():
1380
- # Remember-cookie ์ƒํƒœ ๋“ฑ ๋น„-ํ”„๋ ˆ์‹œ ์„ธ์…˜์ด๋ฉด ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋ฅผ ๋ณด์—ฌ์„œ ์žฌ์ธ์ฆ ์œ ๋„
1381
- print("User authenticated but session not fresh; showing login page for reauthentication")
1382
 
1383
  error = None
1384
  if request.method == 'POST':
@@ -1404,7 +1401,12 @@ def login():
1404
  print(f"Redirecting to: {next_page}")
1405
  return redirect(next_page)
1406
  print("Redirecting to index.html")
1407
- return redirect(url_for('serve_index_html'))
 
 
 
 
 
1408
  else:
1409
  error = 'Invalid username or password'
1410
  print(f"Login failed: {error}")
@@ -1622,15 +1624,15 @@ def serve_static(filename):
1622
 
1623
  # ์ธ๋ฑ์Šค HTML ์ง์ ‘ ์„œ๋น™ (๋กœ๊ทธ์ธ ํ•„์š”)
1624
  @app.route('/index.html')
1625
- @fresh_login_required
1626
  def serve_index_html():
1627
  # ์„ธ์…˜ ๋ฐ ์ฟ ํ‚ค ๋””๋ฒ„๊ทธ ์ •๋ณด
1628
  print(f"Request to /index.html - Session data: {dict(session)}")
1629
  print(f"Request to /index.html - Cookies: {request.cookies}")
1630
  print(f"Request to /index.html - User authenticated: {current_user.is_authenticated}")
1631
 
1632
- # ์ธ์ฆ ํ™•์ธ (fresh session only)
1633
- if not current_user.is_authenticated or not login_fresh():
1634
  print("User not authenticated, redirecting to login")
1635
  return redirect(url_for('login'))
1636
 
 
75
  app.config['CORS_HEADERS'] = 'Content-Type'
76
  # Remember cookie (Flask-Login) โ€” minimize duration to prevent auto re-login
77
  app.config['REMEMBER_COOKIE_DURATION'] = timedelta(seconds=1)
78
+ app.config['REMEMBER_COOKIE_SECURE'] = True # HTTPS required for HF Spaces
79
  app.config['REMEMBER_COOKIE_HTTPONLY'] = True
80
+ app.config['REMEMBER_COOKIE_SAMESITE'] = 'None'
81
+ # Session cookie (Flask-Session) - configured for Hugging Face Spaces
82
+ app.config['SESSION_COOKIE_SECURE'] = True # HTTPS required for HF Spaces
83
  app.config['SESSION_COOKIE_HTTPONLY'] = True
84
+ app.config['SESSION_COOKIE_SAMESITE'] = 'None' # Required for iframe embedding
85
  app.config['SESSION_COOKIE_PATH'] = '/'
86
+ CORS(app, supports_credentials=True) # Enable CORS for all routes with credentials
87
 
88
  # ์‹œํฌ๋ฆฟ ํ‚ค ์„ค์ • (์„ธ์…˜ ์•”ํ˜ธํ™”์— ์‚ฌ์šฉ)
89
  app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'vision_llm_agent_secret_key')
 
1372
 
1373
  @app.route('/login', methods=['GET', 'POST'])
1374
  def login():
1375
+ # ์ด๋ฏธ ๋กœ๊ทธ์ธ๋œ ์‚ฌ์šฉ์ž๋Š” ๋ฉ”์ธ ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋””๋ ‰์…˜ (remove fresh requirement for HF Spaces)
1376
+ if current_user.is_authenticated:
1377
+ print(f"User already authenticated as: {current_user.username}, redirecting to index")
1378
  return redirect('/index.html')
 
 
 
1379
 
1380
  error = None
1381
  if request.method == 'POST':
 
1401
  print(f"Redirecting to: {next_page}")
1402
  return redirect(next_page)
1403
  print("Redirecting to index.html")
1404
+ response = make_response(redirect(url_for('serve_index_html')))
1405
+ # Set additional headers for HF Spaces compatibility
1406
+ response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
1407
+ response.headers['Pragma'] = 'no-cache'
1408
+ response.headers['Expires'] = '0'
1409
+ return response
1410
  else:
1411
  error = 'Invalid username or password'
1412
  print(f"Login failed: {error}")
 
1624
 
1625
  # ์ธ๋ฑ์Šค HTML ์ง์ ‘ ์„œ๋น™ (๋กœ๊ทธ์ธ ํ•„์š”)
1626
  @app.route('/index.html')
1627
+ @login_required
1628
  def serve_index_html():
1629
  # ์„ธ์…˜ ๋ฐ ์ฟ ํ‚ค ๋””๋ฒ„๊ทธ ์ •๋ณด
1630
  print(f"Request to /index.html - Session data: {dict(session)}")
1631
  print(f"Request to /index.html - Cookies: {request.cookies}")
1632
  print(f"Request to /index.html - User authenticated: {current_user.is_authenticated}")
1633
 
1634
+ # ์ธ์ฆ ํ™•์ธ (remove fresh login requirement for HF Spaces)
1635
+ if not current_user.is_authenticated:
1636
  print("User not authenticated, redirecting to login")
1637
  return redirect(url_for('login'))
1638