Skip to content Skip to sidebar Skip to footer

Expire Session In Flask In Ajax Context

I am using permanent_session_lifetime to expire the session of the user after some period of inactivity. The problem is that, this request is made through ajax, so i can't redirect

Solution 1:

If you want to use the before_request approach, then check for the session's validity there and return a redirect as needed:

@mod.before_request
defmake_session_permanent():
    if session_is_invalid(session):
        return redirect(url_for('logout'))

defsession_is_invalid(ses):
    # return your qualifier

Otherwise, swehren's comment is a good idea - instead of relying on the background to redirect the next call, have the Ajax call in the front redirect based on the return valid of the Ajax call:

$.ajax({
    type: "POST",
    url: "{{ url_for('login') }}",
    success: function(data) {
        // redirect if the data contained a qualifier for an expired session
    }
});

Post a Comment for "Expire Session In Flask In Ajax Context"