TypeError: 'bool' object is not callable g.user.is_authenticated()

Dopant Prashant picture Dopant Prashant · Oct 11, 2015 · Viewed 15.6k times · Source

I am trying to do this in my Flask application. But I am getting an error like this

TypeError: 'bool' object is not callable. 

Here is the corresponding code:

@app.before_request
def before_request():
    g.user = current_user
    if g.user.is_authenticated():
       g.search_form = None

Answer

Casimir Crystal picture Casimir Crystal · Oct 11, 2015

Try replace if g.user.is_authenticated(): to if g.user.is_authenticated: like this:

@app.before_request
def before_request():
    g.user = current_user
    if g.user.is_authenticated:
       g.search_form = None

From the document:

is_authenticated

Returns True if the user is authenticated, i.e. they have provided valid credentials. (Only authenticated users will fulfill the criteria of login_required.)

As the document said, is_authenticated is a boolean(True or False).

And however, it was a function in the past, but it has been changed to boolean at version 3.0:

BREAKING:
The is_authenticated, is_active, and is_anonymous
members of the user class are now properties, not methods. Applications should update their user classes accordingly.