Lgiro
Lgiro

Reputation: 772

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 expiration and immediately call a function?

Upvotes: 5

Views: 5541

Answers (2)

Ruthus99
Ruthus99

Reputation: 502

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

Upvotes: 5

Yugam Dhuriya
Yugam Dhuriya

Reputation: 1

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!!!

Upvotes: 0

Related Questions