I am working to try and build a login method for a while now. I am running a Flask app and have it working well. It all runs locally on my machine. Currently, I am using pymongo and MongoClient to make my connection to the DB. This is all working well and I would like to not change this if possible.
I am trying to use Flask-Login to create a users class using usermixin. This is where I have been grossly unsuccessful. I have tried a few different things and my issue is how to I pull the data from my DB. I have done this previously with an SQL DB but for this project I expressly want to use MongoDB. This is the tutorial I was attempting to follow but I am having difficulty understanding everything because it is not explained well what every line is doing.
This is my connection to my DB:
client = MongoClient('mongodb://localhost:27017')
and this is my current users class that I don't have working and where I need the help.
class User(UserMixin):
def __init__(self, username, password_hash):
self.username = username
self.password_hash = password_hash
def check_password(self, password):
return check_password_hash(self.password_hash, password)
def get_id(self):
return self.username
@login_manager.user_loader
def load_user(user_id):
return User.objects(pk=user_id).first()
Then my last part is my login form:
@app.route('/login', methods=["GET" , "POST"])
def login():
if request.method == "GET":
return render_template("login.html", error=False)
if request.method == "POST":
check_user = request.form["username"]
if check_user:
if check_password_hash(check_user['password'], request.form["password"]):
login_user(check_user)
return redirect(url_for('index'))
I am aware that this tutorial uses MongoEngine which I am not using, or not yet but some help here either how to get this code above to work or how to adapt it would be great. When I run this code I am not getting any errors it just doens't work. My test is I try to login and then I try to go to the logout page which is loaded with the following code:
@app.route("/logout")
@login_required
def logout():
logout_user()
return redirect(url_for('index'))
When I do it doesn't load the page and I get and Unauthorized Page notice. Thus I know that my code is not working. Lastly, I have all of templates in a static file location.
Thanks in advance for the help and please if anything is not clear ask and I will try to add more details. The more specific the better I will be able to help.
UPDATE:
I realized that it is also probably important to describe how my DB is structured to make sure that I am accessing it properly because that is a major point where I am having issues. I have a DB with my collection called Users and it is structured with each document being a different user record, like this:
{
"_id" : 1,
"Reset" : false,
"FirstName" : "John",
"LastName" : "Doe",
"Email" : "JohnDoe@gmail.com",
"Username" : "",
"admin" : false,
"Pass" : "[hashed_password]"
}
{
"_id" : 2,
"Reset" : true,
"FirstName" : "Jane",
"LastName" : "Smith",
"Email" : "JaneSmith@hotmail.com",
"Username" : "Jane",
"admin" : false,
"Pass" : "[hashed_password]"
}
{
"_id" : 3,
"Reset" : true,
"FirstName" : "Gary",
"LastName" : "Bettman",
"Email" : "GBettman@yahoo.com",
"Username" : "HockeyGuy",
"admin" : false,
"Pass" : "[hashed_password]"
}