I want to generate a salted password hash and store it in MongoDB collection called users, like this:
users_doc = { "username": "James","password": "<salted_hash_password>"
}
I'm not sure how to generate the hashed password using Bcrypt, then when I login in my flask app, be able to check if the hash matches with the hashed password stored in MongoDB.
I don't know how you use mongodb to bring the data, but if you want to hash the pass it's as easy as:
from flask import Flask
from flask.ext.bcrypt import Bcryptapp = Flask(__name__)
bcrypt = Bcrypt(app)# Your code here...users_doc = {"username": "james","password": bcrypt.generate_password_hash(password)
}
And then if you want to check the password, you can use the check_password_hash()
function:
bcrypt.check_password_hash(users_doc["password"], request.form["password"]) # Just an example of how you could use it.