What if your password database got stolen--would your users still be safe?
Why User model with password in Flask? - Purpose & Use Cases
Imagine building a website where users sign up and log in. You try to store their passwords as plain text in your database.
Every time someone logs in, you check the password by comparing it directly.
Storing passwords as plain text is risky. If someone hacks your database, all user passwords are exposed.
Also, manually checking passwords without hashing is insecure and can lead to data breaches.
Using a User model with password hashing automatically secures passwords by storing only encrypted versions.
This way, even if the database is compromised, real passwords stay safe.
password = request.form['password'] if password == user.password: login_user(user)
from werkzeug.security import check_password_hash if check_password_hash(user.password_hash, request.form['password']): login_user(user)
It enables safe user authentication by protecting passwords with encryption, building trust and security.
Popular websites never store your real password. They use hashed passwords so even if hackers get data, your password stays secret.
Storing plain passwords is unsafe and risky.
User models with password hashing protect user data.
This approach builds secure and trustworthy login systems.
Practice
set_password method in a Flask user model?Solution
Step 1: Understand the role of
This method is used to convert a plain password into a secure hashed version before saving it.set_passwordStep 2: Differentiate from
check_passwordcheck_passwordis for verifying passwords, not setting them.Final Answer:
To hash and store the user's password securely -> Option BQuick Check:
set_passwordhashes password [OK]
- Confusing set_password with check_password
- Thinking set_password shows the password
- Assuming set_password deletes passwords
Solution
Step 1: Identify correct hashing usage
The method must usegenerate_password_hashto hash the password before storing.Step 2: Check assignment to password_hash
The hashed password should be saved inself.password_hash, not plain password.Final Answer:
def set_password(self, password): self.password_hash = generate_password_hash(password) -> Option DQuick Check:
Use generate_password_hash to store hashed password [OK]
- Storing plain password instead of hashed
- Using check_password_hash inside set_password
- Assigning password to wrong attribute
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')Solution
Step 1: Understand password setting
The password 'secret' is hashed and stored inpassword_hash.Step 2: Check password verification
check_passwordcompares the hash with 'secret' and returns True if they match.Final Answer:
True -> Option CQuick Check:
Matching password returns True [OK]
- Expecting False for correct password
- Thinking check_password returns the hash
- Assuming error if password matches
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)
Solution
Step 1: Review check_password_hash usage
The correct order ischeck_password_hash(stored_hash, password).Step 2: Identify argument mistake
The code passespasswordfirst andself.password_hashsecond, which is wrong.Final Answer:
The order of arguments in check_password_hash is reversed -> Option AQuick Check:
check_password_hash needs (hash, password) order [OK]
- Swapping arguments in check_password_hash
- Initializing password_hash as None causes no error but is not best
- Expecting set_password to return a value
- Confusing missing attributes with password errors
change_password to your Flask user model that updates the password only if the old password matches. Which implementation correctly applies this logic?Solution
Step 1: Verify old password before changing
The method must check if the old password matches usingcheck_password.Step 2: Update password only if old password is correct
If the check passes, callset_passwordwith the new password and return True; else return False.Final Answer:
def change_password(self, old_password, new_password): if self.check_password(old_password): self.set_password(new_password) return True return False -> Option AQuick Check:
Change password only if old password matches [OK]
- Changing password without verifying old password
- Comparing old and new passwords incorrectly
- Swapping old and new passwords in checks
Ready to prove it?
Pass the test and this pattern moves unknown
