fix: resolve AttributeError in User model and ensure consistent password handling
- Fixed the `AttributeError: 'User' object has no attribute '_password'` by properly mapping the `_password` attribute to the `password` column in the database. - Updated the `User` model to ensure passwords are only hashed once during creation and not re-hashed when retrieved or updated. - Improved the `check_password` method to correctly compare hashed passwords. - Verified the signup and login flow to ensure consistent behavior
This commit is contained in:
parent
d13c5885d8
commit
4a4d693d72
4 changed files with 36 additions and 19 deletions
|
|
@ -1,27 +1,30 @@
|
|||
from models.User.user import User, db
|
||||
from werkzeug.security import generate_password_hash, check_password_hash
|
||||
|
||||
class UserService:
|
||||
def create_user(self, username, password):
|
||||
if not username or not password:
|
||||
return jsonify({"error": "Username and password are required"}), 400
|
||||
raise ValueError("Username and password are required")
|
||||
|
||||
if len(username) < 3 or len(password) < 8:
|
||||
return jsonify({"error": "Username must be at least 3 characters and password must be at least 8 characters."}), 400
|
||||
|
||||
raise ValueError("Username must be at least 3 characters and password must be at least 8 characters.")
|
||||
|
||||
existing_user = User.query.filter_by(username=username).first()
|
||||
if existing_user:
|
||||
raise ValueError("User already exists")
|
||||
|
||||
hashed_password = generate_password_hash(password)
|
||||
new_user = User(username=username, password=hashed_password)
|
||||
new_user = User(username=username, password=password)
|
||||
db.session.add(new_user)
|
||||
db.session.commit()
|
||||
return new_user
|
||||
|
||||
def verify_user(self, username, password):
|
||||
user = User.query.filter_by(username=username).first()
|
||||
if not user or not user.check_password(password):
|
||||
if not user:
|
||||
print(f"User not found: {username}")
|
||||
raise ValueError("Invalid username or password")
|
||||
|
||||
if not user.check_password(password):
|
||||
raise ValueError("Invalid username or password")
|
||||
|
||||
print(f"User verified: {username}")
|
||||
return user
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue