如何从rails中的用户销毁会话?

时间:2015-09-17 11:17:05

标签: ruby-on-rails ruby-on-rails-4 sorcery

我使用巫术进行登记并使用门卫。我希望在用户处于非活动状态后30分钟后销毁会话。我试过这样但是不起作用。

首先将此子模块添加到巫术中:

配置/初始化/ sorcery.rb

Rails.application.config.sorcery.submodules = [:session_timeout]

会话超时可配置如下:

配置/初始化/ sorcery.rb

Rails.application.config.sorcery.configure do |config|
  config.session_timeout = 3600 # This is in seconds. You could also write 1.hour
  config.session_timeout_from_last_action = true # session timeout is calculated from the last valid activity. By default this is false.
end

1 个答案:

答案 0 :(得分:0)

你可以试试

在控制器中使用这种方式

before_filter :session_expires
before_filter :update_sessions

def session_expires
  @time_left = (session[:expires_at] - Time.now).to_i
  unless @time_left > 0
    reset_session #clear sessions
    redirect_to '/'
  end
end

def update_sessions
  session[:expires_at] = 30.minutes.from_now
end