|
|
@ -105,26 +105,28 @@ def login(): |
|
|
|
""" |
|
|
|
""" |
|
|
|
Handle user login. |
|
|
|
Handle user login. |
|
|
|
""" |
|
|
|
""" |
|
|
|
data = request.json # Expecting JSON body |
|
|
|
data = request.form # Expecting JSON body |
|
|
|
|
|
|
|
|
|
|
|
# Extract credentials from request |
|
|
|
# Extract credentials from request |
|
|
|
username = data.get('username') |
|
|
|
# username = data.get('username') |
|
|
|
|
|
|
|
email = data.get('email') |
|
|
|
password = data.get('password') |
|
|
|
password = data.get('password') |
|
|
|
user_agent = request.headers.get('User-Agent', 'Unknown') |
|
|
|
user_agent = request.headers.get('User-Agent', 'Unknown') |
|
|
|
|
|
|
|
|
|
|
|
# Validate required fields |
|
|
|
# Validate required fields |
|
|
|
if not username or not password: |
|
|
|
if not email or not password: |
|
|
|
return jsonify({"error": "Username and password are required"}), 400 |
|
|
|
return jsonify({"error": "email and password are required"}), 400 |
|
|
|
|
|
|
|
|
|
|
|
# Find the user by username |
|
|
|
# Find the user by username |
|
|
|
user = User.query.filter_by(username=username).first() |
|
|
|
# user = User.query.filter_by(username=username).first() |
|
|
|
|
|
|
|
user = User.query.filter_by(email=email).first() |
|
|
|
|
|
|
|
|
|
|
|
if not user: |
|
|
|
if not user: |
|
|
|
return jsonify({"error": "Invalid username or password"}), 401 |
|
|
|
return jsonify({"error": "Invalid email or password"}), 401 |
|
|
|
|
|
|
|
|
|
|
|
# Verify the password |
|
|
|
# Verify the password |
|
|
|
if not check_password_hash(user.hash_password, password): |
|
|
|
if not check_password_hash(user.hash_password, password): |
|
|
|
return jsonify({"error": "Invalid username or password"}), 401 |
|
|
|
return jsonify({"error": "Invalid email or password"}), 401 |
|
|
|
|
|
|
|
|
|
|
|
# Create a new session |
|
|
|
# Create a new session |
|
|
|
session_key = str(uuid.uuid4()) # Generate a unique session key |
|
|
|
session_key = str(uuid.uuid4()) # Generate a unique session key |
|
|
|