Skip to content Skip to sidebar Skip to footer

Call A Function When Flask Session Expires

In my Flask application, I am saving files that correspond to a user, and want to delete these files when the user's 'session' expires. Is it possible to detect the session expirat

Solution 1:

Had the same issue and solved it not by using the in-built permanent session expiry feature, but added my own key to the session and checking it before every request like follows:

@app.before_request
def before_request()

    now = datetime.datetime.now()
    try:
        last_active = session['last_active']
        delta = now - last_active
        if delta.seconds > 1800:
            session['last_active'] = now
            return logout('Your session has expired after 30 minutes, you have been logged out')
    except:
        pass

    try:
        session['last_active'] = now
    except:
        pass

Solution 2:

Yeah its kind of possible run a loop to see until session['key']==None and if the condition becomes true call the function. I hope this helps!!!


Post a Comment for "Call A Function When Flask Session Expires"