Expire session in flask in ajax context
我正在使用
1 | http://xxxx/login?next=%2Fusers%2Fajax_step1 |
如果Flask会话到期,我想在
1 2 3 4 5 | @mod.before_request def make_session_permanent(): session.modified = True session.permanent = True app.permanent_session_lifetime = timedelta(minutes=10) |
1 2 3 4 5 6 7 | @mod.route('/logout') @login_required def logout(): logout_user() session.clear() flash(gettext(u'You have been logged out.')) return redirect(url_for('auth.login')) |
如果您想使用
1 2 3 4 5 6 7 | @mod.before_request def make_session_permanent(): if session_is_invalid(session): return redirect(url_for('logout')) def session_is_invalid(ses): # return your qualifier |
否则,swehren 的注释是个好主意——不要依赖后台重定向下一个调用,而是根据 Ajax 调用的返回有效在前面重定向 Ajax 调用:
1 2 3 4 5 6 7 | $.ajax({ type:"POST", url:"{{ url_for('login') }}", success: function(data) { // redirect if the data contained a qualifier for an expired session } }); |