Is there an easy way to make sessions timeout in flask? Is there an easy way to make sessions timeout in flask? python python

Is there an easy way to make sessions timeout in flask?


flask sessions expire once you close the browser unless you have a permanent session. You can possibly try the following:

from datetime import timedeltafrom flask import session, app@app.before_requestdef make_session_permanent():    session.permanent = True    app.permanent_session_lifetime = timedelta(minutes=5)

By default in Flask, permanent_session_lifetime is set to 31 days.


Yes, We should set

session.permanent = Trueapp.permanent_session_lifetime = timedelta(minutes=5)

But I don't think it should be set at app.before_request, This will lead to set them too may times.

The permanent_session_lifetime is a Basics Configuration, so it should be set at you configure the app:

 from datetime import timedelta app = Flask(__name__) app.config['SECRET_KEY'] = 'xxxxxxxxx' app.config['PERMANENT_SESSION_LIFETIME'] =  timedelta(minutes=5)

The session will created for each client, seperated from other clients. So, I think the best place to set session.permanent is when you login():

@app.route('/login', methods=['GET', 'POST'])def login():    #After Verify the validity of username and password    session.permanent = True