Flask-login not working as expected Flask-login not working as expected flask flask

Flask-login not working as expected


I tried your code and it does work for me (with changes) - you must have done something wrong outside what you have shown here. In particular, readingtoken was output as expected, and current_user was the logged in user in user view.

In particular, check that you did install the login manager to the application:

app = Flask(__name__)app.config['SECRET_KEY'] = '123123123'login_manager = LoginManager()login_manager.init_app(app)login_manager.session_protection = "strong"login_serializer = URLSafeTimedSerializer(app.secret_key)

and see that the plugin versions are proper; mine are

  • Flask 0.10.1
  • Flask-Login 0.2.11
  • itsdangerous 0.24

Also, you might want to use the TwitterUser with UserMixin instead of wrapping 1 "user" object inside another.


The problem might be that you are overriding the app.session_interface of Flask with a custom session interface.Flask uses the flask.sessions.SecureCookieSessionInterface, and Flask-Login seems to depend on certain properties in it.

Might you by any chance be using MongoDB as your database and/or MongoEngine as the ODM? (It might happen with other app.session_interface overrides as well)I had the exact same problem with token_load not being called, debugged it for about a day, only to find out that the problem was the MongoEngineSessionInterface that I used in the app.

Not exactly sure if it applies here, but in my case the problem was that Flask-Login performs a check in its code for 'user_id' being in the session, and when using MongoEngineSessionInterface it always is, so it never had to call my token loading handler.

Simply removing that session interface as a workaround worked for me.