Spaces:
Running
Running
David Ko
commited on
Commit
ยท
a44b826
1
Parent(s):
51bb097
Fix authentication issues with Flask-Login
Browse files
api.py
CHANGED
@@ -49,6 +49,9 @@ class User(UserMixin):
|
|
49 |
self.id = id
|
50 |
self.username = username
|
51 |
self.password = password
|
|
|
|
|
|
|
52 |
|
53 |
# ํ
์คํธ์ฉ ์ฌ์ฉ์ (์ค์ ํ๊ฒฝ์์๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฌ์ฉ ๊ถ์ฅ)
|
54 |
users = {
|
@@ -56,11 +59,16 @@ users = {
|
|
56 |
'user': User('2', 'user', 'user123')
|
57 |
}
|
58 |
|
|
|
59 |
@login_manager.user_loader
|
60 |
def load_user(user_id):
|
61 |
-
|
|
|
|
|
62 |
if user.id == user_id:
|
|
|
63 |
return user
|
|
|
64 |
return None
|
65 |
|
66 |
# Model initialization
|
@@ -1118,6 +1126,11 @@ LOGIN_TEMPLATE = '''
|
|
1118 |
|
1119 |
@app.route('/login', methods=['GET', 'POST'])
|
1120 |
def login():
|
|
|
|
|
|
|
|
|
|
|
1121 |
error = None
|
1122 |
if request.method == 'POST':
|
1123 |
username = request.form.get('username')
|
@@ -1133,13 +1146,15 @@ def login():
|
|
1133 |
session['username'] = username
|
1134 |
session.permanent = True
|
1135 |
|
1136 |
-
print(f"Login successful for user: {username}")
|
1137 |
|
1138 |
# ๋ฆฌ๋๋ ์
์ฒ๋ฆฌ
|
1139 |
next_page = request.args.get('next')
|
1140 |
if next_page and next_page.startswith('/'):
|
|
|
1141 |
return redirect(next_page)
|
1142 |
-
|
|
|
1143 |
else:
|
1144 |
error = 'Invalid username or password'
|
1145 |
print(f"Login failed: {error}")
|
@@ -1193,9 +1208,19 @@ def status():
|
|
1193 |
"user": current_user.username
|
1194 |
})
|
1195 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1196 |
@app.route('/index')
|
1197 |
@login_required
|
1198 |
-
def
|
1199 |
return send_from_directory('static', 'index.html')
|
1200 |
|
1201 |
if __name__ == "__main__":
|
|
|
49 |
self.id = id
|
50 |
self.username = username
|
51 |
self.password = password
|
52 |
+
|
53 |
+
def get_id(self):
|
54 |
+
return str(self.id) # Flask-Login์ ๋ฌธ์์ด ID๋ฅผ ์๊ตฌํจ
|
55 |
|
56 |
# ํ
์คํธ์ฉ ์ฌ์ฉ์ (์ค์ ํ๊ฒฝ์์๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฌ์ฉ ๊ถ์ฅ)
|
57 |
users = {
|
|
|
59 |
'user': User('2', 'user', 'user123')
|
60 |
}
|
61 |
|
62 |
+
# ์ฌ์ฉ์ ๋ก๋ ํจ์
|
63 |
@login_manager.user_loader
|
64 |
def load_user(user_id):
|
65 |
+
print(f"Loading user with ID: {user_id}")
|
66 |
+
# user_id๊ฐ ๋ฌธ์์ด๋ก ์ ๋ฌ๋๊ธฐ ๋๋ฌธ์ ์ฌ์ฉ์ ์ด๋ฆ์ผ๋ก ์ฒ๋ฆฌ
|
67 |
+
for username, user in users.items():
|
68 |
if user.id == user_id:
|
69 |
+
print(f"User found: {username}")
|
70 |
return user
|
71 |
+
print(f"User not found with ID: {user_id}")
|
72 |
return None
|
73 |
|
74 |
# Model initialization
|
|
|
1126 |
|
1127 |
@app.route('/login', methods=['GET', 'POST'])
|
1128 |
def login():
|
1129 |
+
# ์ด๋ฏธ ๋ก๊ทธ์ธ๋ ์ฌ์ฉ์๋ ๋ฉ์ธ ํ์ด์ง๋ก ๋ฆฌ๋๋ ์
|
1130 |
+
if current_user.is_authenticated:
|
1131 |
+
print(f"User already authenticated as: {current_user.username}, redirecting to index")
|
1132 |
+
return redirect('/index')
|
1133 |
+
|
1134 |
error = None
|
1135 |
if request.method == 'POST':
|
1136 |
username = request.form.get('username')
|
|
|
1146 |
session['username'] = username
|
1147 |
session.permanent = True
|
1148 |
|
1149 |
+
print(f"Login successful for user: {username}, ID: {user.id}")
|
1150 |
|
1151 |
# ๋ฆฌ๋๋ ์
์ฒ๋ฆฌ
|
1152 |
next_page = request.args.get('next')
|
1153 |
if next_page and next_page.startswith('/'):
|
1154 |
+
print(f"Redirecting to: {next_page}")
|
1155 |
return redirect(next_page)
|
1156 |
+
print("Redirecting to index page")
|
1157 |
+
return redirect('/index')
|
1158 |
else:
|
1159 |
error = 'Invalid username or password'
|
1160 |
print(f"Login failed: {error}")
|
|
|
1208 |
"user": current_user.username
|
1209 |
})
|
1210 |
|
1211 |
+
@app.route('/')
|
1212 |
+
def index():
|
1213 |
+
print(f"Root route accessed, user authenticated: {current_user.is_authenticated}")
|
1214 |
+
if current_user.is_authenticated:
|
1215 |
+
print(f"User is authenticated as: {current_user.id}")
|
1216 |
+
return redirect('/index')
|
1217 |
+
else:
|
1218 |
+
print("User is not authenticated, redirecting to login")
|
1219 |
+
return redirect(url_for('login'))
|
1220 |
+
|
1221 |
@app.route('/index')
|
1222 |
@login_required
|
1223 |
+
def index_page():
|
1224 |
return send_from_directory('static', 'index.html')
|
1225 |
|
1226 |
if __name__ == "__main__":
|