otj202
otj202

Reputation: 51

Flask sessions: PERMANENT_SESSION_LIFETIME not being set correctly

So I am using a flask session. I am using the filesystem type so that I can store more session data than I otherwise could. My users want to keep session timeouts long, and the site uses minimal server storage, so this is fine. However, when I try to set the timeout session to 24 hours below, it still times out after 30 minutes.

application = Flask(__name__)
SECRET_KEY = os.urandom(32)
application.config['SESSION_PERMANENT'] = True
application.config['SESSION_TYPE'] = 'filesystem'
application.config['PERMANENT_SESSION_LIFETIME'] = timedelta(hours = 24)

application.config['SECRET_KEY'] = SECRET_KEY

Session(application)



@application.before_request
def make_session_permanent():
    session.permanent = True
    application.permanent_session_lifetime = timedelta(hours = 24)




What am I doing wrong here?

Upvotes: 0

Views: 7694

Answers (2)

otj202
otj202

Reputation: 51

I believe I have found the issue, which was in the line : SECRET_KEY = os.urandom(32). Every time the app restarted (idle window in browser reloaded), session data was erased as a new secret key was generated, meaning the browser could not find the cookies it needed. I generated one key, externally, and then hardcoded this value into my code so it is the same every time.

Upvotes: 2

gittert
gittert

Reputation: 1308

I would change the line with session.permanent =true to session.modified = True and see if it works then.

Upvotes: 1

Related Questions