Python/Flask How to display banner on session expiration

82 Views Asked by At

I am working on a web application in Flask, and I want to display a banner to the user whenever the session times out. For the banner I am using the Flask's flash("Session expired", "danger") method, but the problem I am having is displaying the banner after the session refreshes.

I am using code similar to the following to set the session expiration time:

@app.before_request
def set_timeout():
    session.permanent = True
    app.permanent_session_lifetime = timedelta(seconds=60)
    session.modified = True

This works for expiring the session after the set period of inactivity, but I can't seem to display my flash() banner appropriately. Is there an easy way to check if the session has expired and display the banner accordingly? Something like:

@app.before_request
def show_banner():
    if previous_session_expired: # <- How do I calculate this?
        flash("Session expired", "danger")
0

There are 0 best solutions below