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:
Blake Ridgway 2025-02-15 22:42:50 -06:00
parent d13c5885d8
commit 4a4d693d72
4 changed files with 36 additions and 19 deletions

View file

@ -1,22 +1,23 @@
from models import db
from werkzeug.security import generate_password_hash, check_password_hash
from models import db
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
password = db.Column(db.String(128), nullable=False)
_password = db.Column("password", db.String(255), nullable=False)
def __init__(self, username, password, hash_password=True):
self.username = username
if hash_password:
self.password = generate_password_hash(password, method="pbkdf2:sha256")
@property
def password(self):
return self._password
@password.setter
def password(self, raw_password):
if not raw_password.startswith("pbkdf2:sha256:"):
self._password = generate_password_hash(raw_password)
else:
self.password = password
self._password = raw_password
def check_password(self, password):
return check_password_hash(self.password, password)
def __repr__(self):
return f"<User {self.username}>"
return check_password_hash(self._password, password)