Sometimes, we want to make sessions timeout in Python Flask.
In this article, we’ll look at how to make sessions timeout in Python Flask.
How to make sessions timeout in Python Flask?
To make sessions timeout in Python Flask, we set the session.permanent
and app.permanent_session_lifetime
properties.
For instance, we write
from datetime import timedelta
from flask import session, app
@app.before_request
def make_session_permanent():
session.permanent = True
app.permanent_session_lifetime = timedelta(minutes=5)
We set make a session permanent with
session.permanent = True
And then we set to session lifetime to 5 minutes with
app.permanent_session_lifetime = timedelta(minutes=5)
We use the @app.before_request
decorator to run make_session_permanent
before every request.
Conclusion
To make sessions timeout in Python Flask, we set the session.permanent
and app.permanent_session_lifetime
properties.