3

I'm using Flask-Login and I want to check if a user is logged-in by calling current_user.is_authenticated().

My Flask application is organized into Blueprints with a main __init__.py file that sets up my app, sets up Blueprints, and initializes Flask-Login. I have a script, run.py, located up one directory from __init__.py that instantiates the application and runs it if in debug mode.

__init.py__ looks (in part) like this:

from flask import Flask, session, request, render_template                                                            
from flask.ext.login import LoginManager, current_user

from panel.views import dash, projects

from panel.users.models import User, AnonymousUser

app = Flask(__name__)
app.config.from_object("configure") 

login_manager = flask_login.LoginManager()
login_manager.init_app(app)
login_manager.login_view = "/signin"
login_manager.anonymous_user = AnonymousUser
@login_manager.user_loader
def load_user(str_id):
    return User.user_loader(str_id)

# Register all blueprints here.
app.register_blueprint(dash.bp, url_prefix="/dash")
app.register_blueprint(projects.bp, url_prefix="/projects")

logging.warning(current_user.is_authenticated())

When I run this, from run.py (which looks, in part, like this):

from panel import app 

app.run(host='0.0.0.0', debug=True)

This Exception is raised:

Traceback (most recent call last):
  File "run.py", line 5, in <module>
    from panel import app
  File "/opt/www/panel/panel/__init__.py", line 40, in <module>
    logging.warning(current_user.is_authenticated())
  File "/opt/virtualenvs/coefficient/lib/python2.6/site-packages/werkzeug/local.py", line     336, in __getattr__
    return getattr(self._get_current_object(), name)
  File "/opt/virtualenvs/coefficient/lib/python2.6/site-packages/werkzeug/local.py", line 295, in _get_current_object
    return self.__local()
  File "/opt/virtualenvs/coefficient/lib/python2.6/site-packages/flask_login.py", line 403, in <lambda>
    current_user = LocalProxy(lambda: _request_ctx_stack.top.user)
AttributeError: 'NoneType' object has no attribute 'user'

Because I omitted some lines for brevity, the line numbers in that traceback do not match the code I posted, but you can see that the line logging.warning(current_user.is_authenticated()) seems to be where the error occurs.

Any help is appreciated.

skyler
  • 8,010
  • 15
  • 46
  • 69
  • 1
    Looks like you're not inside a request so there is no current user? – agf Oct 03 '12 at 14:00
  • @agf you're probably correct. I'll try this and post a follow-up. – skyler Oct 03 '12 at 14:39
  • Try to execute logging.warning(current_user.is_authenticated()) after app.run() (I mean in some blueprint method or the code which is callable). I think request could be created after app is running and getting the request. – Ignas Butėnas Oct 03 '12 at 18:45
  • @agf I'm calling current_user.is_authenticated() in a decorator in one of my files, and I can't control when the request context is set up. Because it's a decorator, it must be executing (I assume) after the app is instantiated, but before run() is called. – skyler Oct 04 '12 at 16:04
  • @skyler Even after `run` is called, there won't be a current request outside of a method. – agf Oct 04 '12 at 16:57
  • @agf As you suspected, the problem was that I was not within a request context. If you'll add this as an answer I'll accept it. – skyler Oct 04 '12 at 21:41

2 Answers2

8

You can't call

current_user.is_authenticated()

outside of a method/view, because the code outside of the methods/views will be run outside of a request.

If you're not in a request, there is no current_user on which to check authentication.

simanacci
  • 2,197
  • 3
  • 26
  • 35
agf
  • 171,228
  • 44
  • 289
  • 238
0

May be you can try

login_manager.init_app(app)

Flask-Login

Tunaki
  • 132,869
  • 46
  • 340
  • 423