Performance: User model with password
This affects page load speed and interaction responsiveness when user authentication is involved, especially during login and registration.
from werkzeug.security import generate_password_hash, check_password_hash class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True, nullable=False) password_hash = db.Column(db.String(128), nullable=False) def set_password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password)
class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True, nullable=False) password = db.Column(db.String(80), nullable=False) # storing plain text password def check_password(self, password): return self.password == password
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Plain text password storage and check | None (server-side) | None | None | [X] Bad |
| Hashed password storage with werkzeug | None (server-side) | None | None | [OK] Good |
set_password method in a Flask user model?set_passwordcheck_passwordcheck_password is for verifying passwords, not setting them.set_password hashes password [OK]generate_password_hash to hash the password before storing.self.password_hash, not plain password.user.check_password('secret') return if the password was set with user.set_password('secret')?
from werkzeug.security import generate_password_hash, check_password_hash
class User:
def __init__(self):
self.password_hash = ''
def set_password(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)
user = User()
user.set_password('secret')password_hash.check_password compares the hash with 'secret' and returns True if they match.from werkzeug.security import generate_password_hash, check_password_hash
class User:
def __init__(self):
self.password_hash = None
def set_password(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(password, self.password_hash)
check_password_hash(stored_hash, password).password first and self.password_hash second, which is wrong.change_password to your Flask user model that updates the password only if the old password matches. Which implementation correctly applies this logic?check_password.set_password with the new password and return True; else return False.Pass the test and this pattern moves unknown