【问题标题】:How to avoid to check user status on every page?如何避免在每个页面上检查用户状态?
【发布时间】:2018-04-10 05:40:13
【问题描述】:

我可以检查用户并在某些情况下注销。 但总有一个像下面这样的 ApplicationController 检查。

before_action :authenticate_user!, :check_should_logout

我不想在每次请求时都致电check_should_logout

有什么解决办法吗?

【问题讨论】:

  • check_should_logout 的目标是什么?
  • check_should_logout 的目标是检查会话已经过期的用户。系统中有会话已过期的用户。我可以强制注销用户并通过 check_should_logout 方法进行检查。但是,我不想在每个请求(在控制器上)上调用 check_should_logout 方法。有没有办法在控制器上使用 except 或 only 动作?

标签: ruby-on-rails devise ruby-on-rails-5


【解决方案1】:

您可以在各自的控制器类中为before_action 过滤器使用onlyexcept 选项。

before_action :check_should_logout, only: [:action_1, :action_2]

before_action :check_should_logout, except: [:action_1, :action_2]

【讨论】:

  • 感谢您的回复。有很多动作。我认为使用except似乎不合适。如果我无法解决这个问题,我将每隔 5 或 10 分钟批量检查一次,如果登录用户(会话已过期)调用 log_out 函数。
【解决方案2】:
before_action :authenticate_user!, :check_should_logout, only: [:action1, :action_2]

before_action :authenticate_user!, :check_should_logout, except: [:action1, :action_2]

与其在 ApplicationController 中提供,不如在各自的控制器中提供。

【讨论】:

  • 感谢您的回复。有很多动作。我认为使用except似乎不合适。如果我无法解决这个问题,我将每隔 5 或 10 分钟批量检查一次,如果登录用户(会话已过期)调用 log_out 函数。
猜你喜欢
  • 2017-01-17
  • 1970-01-01
  • 2016-07-20
  • 2013-01-02
  • 2023-01-25
  • 2021-06-22
  • 1970-01-01
  • 2021-11-16
  • 2011-07-02
相关资源
最近更新 更多