Why isn't my current_user authenticated in flask-login? Why isn't my current_user authenticated in flask-login? flask flask

Why isn't my current_user authenticated in flask-login?


You have a method named User.is_authenticated. Inside User.__init__, though, you set an attribute with the same name.

self.is_authenticated = False

This overrides the method. Then, whenever you check current_user.is_authenticated, you are accessing the attribute that's always false.

You should remove the assignment from __init__ and change is_authenticated to the following:

def is_authenticated(self):    return True

If you need it to be dynamic for some reason, rename the attribute so it doesn't shadow the method.

def is_authenticated(self):    return self._authenticated

Another problem is with your load_user function.

Instead of filtering for User.id==user_id, you are getting it. The user wasn't being returned because load_user is returning User.query.get(True) instead of User.query.get(user_id).

If you make the following change, it will work:

@login_manager.user_loaderdef load_user(user_id):    try:        return User.query.get(user_id)    except:        return None


  1. You don't have to create is_authenticated field in Database
  2. Flask-Login will take care of that