- 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
23 lines
No EOL
758 B
Python
23 lines
No EOL
758 B
Python
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("password", db.String(255), nullable=False)
|
|
|
|
@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 = raw_password
|
|
|
|
def check_password(self, password):
|
|
return check_password_hash(self._password, password) |