What is the purpose of hashing passwords in a User model?
Hashing passwords protects user data by converting the password into a secure, unreadable format. This way, even if the database is compromised, the actual passwords are not exposed.
Click to reveal answer
beginner
Which Flask extension is commonly used to hash passwords securely?
Flask-Bcrypt is commonly used to hash passwords securely in Flask applications. It provides easy-to-use methods to hash and check passwords.
Click to reveal answer
beginner
In a Flask User model, why should you never store the password as plain text?
Storing passwords as plain text is risky because if the database is leaked, attackers get direct access to user passwords. Hashing prevents this by storing only the hashed version.
Click to reveal answer
intermediate
What is the typical method name used in a Flask User model to set a password securely?
A typical method is called set_password. It takes the plain password, hashes it, and stores the hashed password in the model.
Click to reveal answer
intermediate
How do you verify a user's password during login in a Flask app?
You use a method like check_password that compares the hashed password stored in the database with the hash of the password entered by the user. If they match, the password is correct.
Click to reveal answer
Which of these is the safest way to store user passwords in Flask?
AHash passwords using Flask-Bcrypt
BStore passwords as plain text
CEncrypt passwords with reversible encryption
DStore passwords in cookies
✗ Incorrect
Hashing passwords with Flask-Bcrypt is the safest because it is one-way and secure. Plain text and reversible encryption are unsafe.
What does the set_password method usually do in a Flask User model?
AHashes the password and stores it
BStores the password as plain text
CDeletes the password
DSends the password by email
✗ Incorrect
set_password hashes the password before storing it to keep it secure.
Which Flask extension helps with password hashing?
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.