【问题标题】:Execute some code when a session timeout using devise is detected?当检测到使用设计的会话超时时执行一些代码?
【发布时间】:2017-01-09 21:58:44
【问题描述】:

我正在使用 Devise 进行身份验证。当我们如下启用 timeoutable 模块时,会话超时功能被启用。

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  devise :database_authenticatable, :registerable, :timeoutable
end

我正在编写一个简单的多人房间逻辑,因此我有一个房间列表,其中显示了房间列表以及当前居住在每个房间中的用户。我的目标是,一旦某个用户的会话由于不活动而过期,就应该检测到该事件,并且应该执行将用户从房间中清除的代码。简而言之,我想知道是否可以检测会话超时并在发生这种情况时执行代码?

感谢您的帮助!谢谢!

【问题讨论】:

标签: ruby-on-rails devise


【解决方案1】:

我想你最好的办法是看看这个问题:

Detect when a devise session expires

他们在哪里重写 timeoutable 方法并实现自己的代码:

module Devise
  module Models
    module Timeoutable
      def timedout?(last_access)
        #  your custom code goes into this method
        return false if remember_exists_and_not_expired?
        !timeout_in.nil? && last_access && last_access <= timeout_in.ago
      end
    end
  end
end

【讨论】:

  • 谢谢,但是这段代码应该放在哪里,即哪个文件?谢谢!
猜你喜欢
  • 2018-08-20
  • 1970-01-01
  • 2012-10-15
  • 2014-05-23
  • 2012-04-22
  • 1970-01-01
  • 2011-12-29
  • 1970-01-01
  • 2010-11-26
相关资源
最近更新 更多