会话超时消息RoR中使用设计 [英] Session Timeout Message in RoR using Devise

查看:200
本文介绍了会话超时消息RoR中使用设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有固定用装置和后30分钟的会话超时的应用程序。随着色器件一切工作正常进行正常航行,如果用户点击一个链接时,超时他们重定向回一条信息登录屏幕您的会话已过期,请重新登录才能继续。,优秀的。

I have an application secured with devise and a session timeout of 30 minutes. With devise all is working well for normal navigation, if the user clicks on a link when timed out they get redirected back to the login screen with a message saying "Your session expired, please sign in again to continue.", excellent.

不过,我有阿贾克斯在很多地方。如果会话超时,用户做一个ajax操作我想同样的行为上面,而那失败默默在后台一个401错误。

However I have ajax in a lot of places. If the session times out and the user does an ajax operation I want the same behavior as above rather that silently failing with a 401 error in the background.

到目前为止,我有这个jQuery code每一页上赶上401S和重装:

So far I have this jquery code on each page to catch 401s and reload:

$(document).ajaxError(function(e, error) {
  switch(error.status) {

    case 401: {
      // unauthorised (possible timeout)
      location.reload();
      break;
    }

它工作得很好,但有一个恼人的问题:

It works well enough, but there is an annoying issue:

由于Ajax请求命中设计首先,当我的应用程序重新加载页面我得到定向到登录页面,但我没有看到超时消息,我看到了未经认证的您需要登录或继续之前注册。消息。

Because the ajax request hits devise first, when my application reloads the page I get directed to the login page, but I don't see the timeout message, I see the unauthenticated "You need to sign in or sign up before continuing." message.

有没有一种方法,以确保在第二个请求色器件仍显示超时消息?

Is there a way to ensure that on the second request devise still shows the timeout message?

推荐答案

我认为你需要通过重定向到特定的URL或设置一些查询参数手动显示该信息,例如。

i think that you need to manually show this message, for example by redirecting to a specific url or setting some query parameter.

问题是,你的401请求后,你有一个新的会话,当你重新加载浏览器,下一个请求会打你的应用程序有一个空会话。这就是为什么你得到的未经验证的消息。

the problem is, that after your 401 request you got a new session and when you reload the browser, the next request will hit your application with an empty session. that's why you get the unauthenticated message.

这篇关于会话超时消息RoR中使用设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆