Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -195,28 +195,29 @@ def home():
|
|
| 195 |
@app.route("/login", methods=["GET", "POST"])
|
| 196 |
def login():
|
| 197 |
if request.method == "POST":
|
|
|
|
| 198 |
data = request.json
|
| 199 |
username = data.get('username', '').strip()
|
| 200 |
password = data.get('password', '')
|
| 201 |
|
| 202 |
users = load_users()
|
| 203 |
-
|
| 204 |
if username in users:
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
if check_password_hash(
|
| 210 |
-
session['username'] = username
|
| 211 |
-
return jsonify({"success": True})
|
| 212 |
-
else:
|
| 213 |
-
# Plaintext password (legacy users) - hash it on login
|
| 214 |
-
if stored_password == password:
|
| 215 |
-
# Update to hashed password
|
| 216 |
-
users[username]['password'] = generate_password_hash(password)
|
| 217 |
-
save_users(users)
|
| 218 |
session['username'] = username
|
| 219 |
return jsonify({"success": True})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
|
| 221 |
return jsonify({"success": False, "message": "Invalid credentials!"})
|
| 222 |
|
|
|
|
| 195 |
@app.route("/login", methods=["GET", "POST"])
|
| 196 |
def login():
|
| 197 |
if request.method == "POST":
|
| 198 |
+
# --- replace your /login POST logic with this ---
|
| 199 |
data = request.json
|
| 200 |
username = data.get('username', '').strip()
|
| 201 |
password = data.get('password', '')
|
| 202 |
|
| 203 |
users = load_users()
|
|
|
|
| 204 |
if username in users:
|
| 205 |
+
stored = users[username]['password']
|
| 206 |
+
|
| 207 |
+
# 1) Try hash-based verification (works for any Werkzeug scheme)
|
| 208 |
+
try:
|
| 209 |
+
if check_password_hash(stored, password):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
session['username'] = username
|
| 211 |
return jsonify({"success": True})
|
| 212 |
+
except Exception:
|
| 213 |
+
pass # if stored isn't a hash string, we'll try plaintext next
|
| 214 |
+
|
| 215 |
+
# 2) Legacy plaintext fallback → upgrade to a hash
|
| 216 |
+
if stored == password:
|
| 217 |
+
users[username]['password'] = generate_password_hash(password)
|
| 218 |
+
save_users(users)
|
| 219 |
+
session['username'] = username
|
| 220 |
+
return jsonify({"success": True})
|
| 221 |
|
| 222 |
return jsonify({"success": False, "message": "Invalid credentials!"})
|
| 223 |
|