ruby-on-rails 如何获取Devise会话超时回调?

eulz3vhy  于 12个月前  发布在  Ruby
关注(0)|答案(3)|浏览(80)

当用户与Devise的会话超时时,是否有方法获得回调/处理某些操作?
重写DeviseSessionsController的普通旧方法不起作用:

class SessionsController < Devise::SessionsController

  def destroy
    #do something
    super
  end

end

字符串
这只在用户注销时起作用,这是有意义的,因为它看起来不像是在会话超时时调用控制器。有人能帮帮我吗?

4nkexdtk

4nkexdtk1#

我发现执行Warden.before_logout是最好的解决方案:

# app/models/user.rb

Warden::Manager.before_logout do |user, auth, opts|
  #fdsafdsafdsa
end

字符串
不幸的是,似乎没有任何方法可以使用纯Devise来做到这一点。

zvokhttg

zvokhttg2#

before_filter :destroy_custom, :only => [ :destroy ]

def destroy_custom 
    # Do your thang
end

字符串
我能用新方法做到这一点。我猜也可以用destroy来做同样的事情。回调可以在devise_custom内部调用,或者devise_custom本身可以是您希望在销毁之前执行某些操作的方法。

eqzww0vc

eqzww0vc3#

这个问题仍然是最新的。我能想到的最简单的解决方案是覆盖User模型中的timedout?方法。

def timedout?(args)
    retval = super(args)
    if retval 
        # Do whatever you need to do here
    end 
    retval
end

字符串
record.timedout?在超时时触发注销之前被调用https://github.com/heartcombo/devise/blob/main/lib/devise/hooks/timeoutable.rb#L27C5-L27C5

相关问题