[Flask] Flask-Login and Flask-Session

spaceman spaceman at antispaceman.com
Fri Feb 16 07:42:59 EST 2018


I wonder if anyone can help me as my knowledge of python isn't good
enough figure this out:

I am using Flask-Session with the RedisSessionInterface
(works by itself) but I also trying to use Flask-Login
with a custom session interface as specified here:

https://flask-login.readthedocs.io/en/latest/#disabling-session-cookie-for-apis

I am pretty certain the code specified there won't work with the
RedisSessionInterface as it is using flask's default session interface. I
tried this:

class CustomSessionInterface(RedisSessionInterface):
    """Prevent creating session from API requests."""
    def save_session(self, *args, **kwargs):
        if g.get('login_via_header'):
            return
        return super(CustomSessionInterface, self).save_session(*args,
                                                                **kwargs)

But the RedisSessionInterface requires arguments (three to be exact)
to configure it properly. My initialization looks like:

...
from flask_login import LoginManager
from flask_redis import Redis
from flask_session import Session, RedisSessionInterface
...
redis.init_app(app)
app.config['SESSION_REDIS'] = app.extensions['redis']['REDIS']
session.init_app(app)
app.session_interface = CustomSessionInterface()
login_manager.init_app(app)
...

I need to take the session interface that initialized by Flask-Session
and turn it into a CustomSessionInterface with the login_via_header
set so that my api won't return a session cookie but still use the
RedisSessionInterface, and my simple brain can't do that.

Regards,
spaceman


More information about the Flask mailing list