Spaces:
Running
Running
David Ko
commited on
Commit
ยท
8fdf1e4
1
Parent(s):
bbbd5f6
Fix login redirect loop with improved session persistence
Browse files
api.py
CHANGED
@@ -4,6 +4,7 @@ import os
|
|
4 |
os.environ['MPLCONFIGDIR'] = '/tmp/matplotlib'
|
5 |
|
6 |
from flask import Flask, request, jsonify, send_from_directory, redirect, url_for, session, render_template_string
|
|
|
7 |
import torch
|
8 |
from PIL import Image
|
9 |
import numpy as np
|
@@ -30,6 +31,11 @@ import chromadb
|
|
30 |
from chromadb.utils import embedding_functions
|
31 |
|
32 |
app = Flask(__name__, static_folder='static')
|
|
|
|
|
|
|
|
|
|
|
33 |
CORS(app) # Enable CORS for all routes
|
34 |
|
35 |
# ์ํฌ๋ฆฟ ํค ์ค์ (์ธ์
์ํธํ์ ์ฌ์ฉ)
|
@@ -53,6 +59,7 @@ app.config['SESSION_TYPE'] = 'filesystem'
|
|
53 |
app.config['SESSION_PERMANENT'] = True
|
54 |
app.config['SESSION_USE_SIGNER'] = True
|
55 |
app.config['SESSION_FILE_DIR'] = session_dir
|
|
|
56 |
print(f"Using session directory: {session_dir}")
|
57 |
Session(app)
|
58 |
|
@@ -1154,10 +1161,11 @@ def login():
|
|
1154 |
if username in users and users[username].password == password:
|
1155 |
# ๋ก๊ทธ์ธ ์ฑ๊ณต ์ ์ธ์
์ ์ฌ์ฉ์ ์ ๋ณด ์ ์ฅ
|
1156 |
user = users[username]
|
1157 |
-
login_user(user, remember=True)
|
1158 |
session['user_id'] = user.id
|
1159 |
session['username'] = username
|
1160 |
session.permanent = True
|
|
|
1161 |
|
1162 |
print(f"Login successful for user: {username}, ID: {user.id}")
|
1163 |
|
@@ -1189,7 +1197,13 @@ def serve_static(filename):
|
|
1189 |
@app.route('/index.html')
|
1190 |
@login_required
|
1191 |
def serve_index_html():
|
1192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
1193 |
return send_from_directory(app.static_folder, 'index.html')
|
1194 |
|
1195 |
# ๊ธฐ๋ณธ ๊ฒฝ๋ก ๋ฐ ๊ธฐํ ๊ฒฝ๋ก ์ฒ๋ฆฌ (๋ก๊ทธ์ธ ํ์)
|
|
|
4 |
os.environ['MPLCONFIGDIR'] = '/tmp/matplotlib'
|
5 |
|
6 |
from flask import Flask, request, jsonify, send_from_directory, redirect, url_for, session, render_template_string
|
7 |
+
from datetime import timedelta
|
8 |
import torch
|
9 |
from PIL import Image
|
10 |
import numpy as np
|
|
|
31 |
from chromadb.utils import embedding_functions
|
32 |
|
33 |
app = Flask(__name__, static_folder='static')
|
34 |
+
app.secret_key = 'your_secret_key_here' # ์ธ์
์ํธํ๋ฅผ ์ํ ๋น๋ฐ ํค
|
35 |
+
app.config['CORS_HEADERS'] = 'Content-Type'
|
36 |
+
app.config['REMEMBER_COOKIE_DURATION'] = timedelta(days=30) # ์ฟ ํค ์ง์ ์๊ฐ
|
37 |
+
app.config['REMEMBER_COOKIE_SECURE'] = False # ๊ฐ๋ฐ ํ๊ฒฝ์์๋ False, ํ๋ก๋์
์์๋ True๋ก ์ค์
|
38 |
+
app.config['REMEMBER_COOKIE_HTTPONLY'] = True
|
39 |
CORS(app) # Enable CORS for all routes
|
40 |
|
41 |
# ์ํฌ๋ฆฟ ํค ์ค์ (์ธ์
์ํธํ์ ์ฌ์ฉ)
|
|
|
59 |
app.config['SESSION_PERMANENT'] = True
|
60 |
app.config['SESSION_USE_SIGNER'] = True
|
61 |
app.config['SESSION_FILE_DIR'] = session_dir
|
62 |
+
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=7) # ์ธ์
์ ํจ ๊ธฐ๊ฐ ์ค์
|
63 |
print(f"Using session directory: {session_dir}")
|
64 |
Session(app)
|
65 |
|
|
|
1161 |
if username in users and users[username].password == password:
|
1162 |
# ๋ก๊ทธ์ธ ์ฑ๊ณต ์ ์ธ์
์ ์ฌ์ฉ์ ์ ๋ณด ์ ์ฅ
|
1163 |
user = users[username]
|
1164 |
+
login_user(user, remember=True, duration=timedelta(days=7)) # remember me ๊ธฐ๋ฅ ํ์ฑํ ๋ฐ ๊ธฐ๊ฐ ์ค์
|
1165 |
session['user_id'] = user.id
|
1166 |
session['username'] = username
|
1167 |
session.permanent = True
|
1168 |
+
session.modified = True # ์ธ์
๋ณ๊ฒฝ ์ฌํญ ์ฆ์ ์ ์ฉ
|
1169 |
|
1170 |
print(f"Login successful for user: {username}, ID: {user.id}")
|
1171 |
|
|
|
1197 |
@app.route('/index.html')
|
1198 |
@login_required
|
1199 |
def serve_index_html():
|
1200 |
+
if not current_user.is_authenticated:
|
1201 |
+
print("User not authenticated, redirecting to login")
|
1202 |
+
return redirect(url_for('login'))
|
1203 |
+
|
1204 |
+
print(f"Serving index.html for authenticated user: {current_user.username}")
|
1205 |
+
# ์ธ์
์ํ ๋๋ฒ๊ทธ
|
1206 |
+
print(f"Session data: user_id={session.get('user_id')}, username={session.get('username')}, is_permanent={session.get('permanent', False)}")
|
1207 |
return send_from_directory(app.static_folder, 'index.html')
|
1208 |
|
1209 |
# ๊ธฐ๋ณธ ๊ฒฝ๋ก ๋ฐ ๊ธฐํ ๊ฒฝ๋ก ์ฒ๋ฆฌ (๋ก๊ทธ์ธ ํ์)
|