David Ko commited on
Commit
8fdf1e4
ยท
1 Parent(s): bbbd5f6

Fix login redirect loop with improved session persistence

Browse files
Files changed (1) hide show
  1. api.py +16 -2
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
- print(f"Serving index.html for user: {current_user.username if current_user.is_authenticated else 'not authenticated'}")
 
 
 
 
 
 
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
  # ๊ธฐ๋ณธ ๊ฒฝ๋กœ ๋ฐ ๊ธฐํƒ€ ๊ฒฝ๋กœ ์ฒ˜๋ฆฌ (๋กœ๊ทธ์ธ ํ•„์š”)