📖 Scenario: You are building a simple Flask app that needs to store user information securely. You want to create a user model that keeps usernames and hashed passwords.
🎯 Goal: Create a Flask user model class called User with attributes for username and password_hash. Implement a method to set the password by hashing it and a method to check a password against the stored hash.
📋 What You'll Learn
Create a User class with username and password_hash attributes
Add a method set_password(password) that hashes the password and stores it in password_hash
Add a method check_password(password) that returns True if the password matches the stored hash
Use werkzeug.security functions generate_password_hash and check_password_hash
💡 Why This Matters
🌍 Real World
Web applications need to store user passwords securely to protect user accounts and data.
💼 Career
Understanding how to hash and verify passwords is essential for backend developers working on user authentication.
Progress0 / 4 steps
1
Create the User class with username and password_hash attributes
Create a class called User with an __init__ method that takes username and sets self.username to it. Also initialize self.password_hash to None.
Flask
Hint
Define a class named User. Inside __init__, assign username to self.username and set self.password_hash to None.
2
Import password hashing functions
Add an import statement to import generate_password_hash and check_password_hash from werkzeug.security.
Flask
Hint
Use from werkzeug.security import generate_password_hash, check_password_hash to import the needed functions.
3
Add set_password method to hash and store password
Inside the User class, add a method called set_password that takes password as a parameter. Use generate_password_hash(password) to create a hash and assign it to self.password_hash.
Flask
Hint
Define set_password method that sets self.password_hash using generate_password_hash(password).
4
Add check_password method to verify password
Inside the User class, add a method called check_password that takes password as a parameter and returns the result of check_password_hash(self.password_hash, password).
Flask
Hint
Define check_password method that returns check_password_hash(self.password_hash, password).
Practice
(1/5)
1. What is the main purpose of the set_password method in a Flask user model?
easy
A. To delete the user's password from the database
B. To hash and store the user's password securely
C. To check if the password entered matches the stored password
D. To display the user's password in plain text
Solution
Step 1: Understand the role of set_password
This method is used to convert a plain password into a secure hashed version before saving it.
Step 2: Differentiate from check_password
check_password is for verifying passwords, not setting them.
Final Answer:
To hash and store the user's password securely -> Option B
A. The order of arguments in check_password_hash is reversed
B. password_hash should be initialized as an empty string, not None
C. set_password should return the hashed password
D. The class is missing a username attribute
Solution
Step 1: Review check_password_hash usage
The correct order is check_password_hash(stored_hash, password).
Step 2: Identify argument mistake
The code passes password first and self.password_hash second, which is wrong.
Final Answer:
The order of arguments in check_password_hash is reversed -> Option A
Quick Check:
check_password_hash needs (hash, password) order [OK]
Hint: check_password_hash(hash, password) order matters [OK]
Common Mistakes:
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
5. You want to add a method change_password to your Flask user model that updates the password only if the old password matches. Which implementation correctly applies this logic?
hard
A. def change_password(self, old_password, new_password):
if self.check_password(old_password):
self.set_password(new_password)
return True
return False
B. def change_password(self, old_password, new_password):
self.set_password(new_password)
return True
C. def change_password(self, old_password, new_password):
if old_password == new_password:
self.set_password(new_password)
return True
return False
D. def change_password(self, old_password, new_password):
if not self.check_password(new_password):
self.set_password(old_password)
return True
return False
Solution
Step 1: Verify old password before changing
The method must check if the old password matches using check_password.
Step 2: Update password only if old password is correct
If the check passes, call set_password with 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 A
Quick Check:
Change password only if old password matches [OK]
Hint: Always verify old password before setting new one [OK]
Common Mistakes:
Changing password without verifying old password
Comparing old and new passwords incorrectly
Swapping old and new passwords in checks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.