Spaces:
Running
Running
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
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'] =
|
79 |
app.config['REMEMBER_COOKIE_HTTPONLY'] = True
|
80 |
-
app.config['REMEMBER_COOKIE_SAMESITE'] = '
|
81 |
-
# Session cookie (Flask-Session) -
|
82 |
-
app.config['SESSION_COOKIE_SECURE'] =
|
83 |
app.config['SESSION_COOKIE_HTTPONLY'] = True
|
84 |
-
app.config['SESSION_COOKIE_SAMESITE'] = '
|
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
|
1377 |
-
print(f"User already authenticated
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
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 |
-
@
|
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
|
1633 |
-
if not current_user.is_authenticated
|
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 |
|