简体   繁体   中英

How to dynamically set the Flask Session timeout?

In my Flask application I typically set the timeout for the session in the beginning of the code:

session.permanent = True
app.permanent_session_lifetime = timedelta(minutes=5)

I need to set the timeout depending on the user's permissions after logging in. Where is the best place to add this code so that I can dynamically change the session lifetime variable? I was thinking maybe after_request, but I only need this to run after a single particular login request....

I would recommend adding it at before_request and checking permissions on each request to set the timeout.

@app.before_request
def make_session_permanent():
    session.permanent = True
    app.permanent_session_lifetime = timedelta(minutes=5)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM