DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Rails Session Timeout

05.28.2009
| 15179 views |
  • submit to reddit
        Adicionando Timeout na sessão do Rails.

Em application_controller.rb:
before_filter :session_expiry, :except => [:login, :logout]
before_filter :update_activity_time, :except => [:login, :logout]

def session_expiry
  @time_left = (session[:expires_at] - Time.now).to_i
  unless @time_left > 0
    reset_session
    flash[:error] = 'Sua sessão expirou. Favor entrar novamente.'
    redirect_to :controller => 'login', :action => 'login'
  end
end

def update_activity_time
  session[:expires_at] = 60.minutes.from_now
end

Em login_controller.rb:
update_activity_time
    

Comments

Snippets Manager replied on Sat, 2010/09/25 - 4:52am

Nice snippet to expire sessions. However, a couple of checks and changes helped me in my project: before_filter :update_activity_time, :except => [:login, :logout] def session_expiry reset_session flash[:error] = 'Your session has expired. Please come again.' redirect_to :controller => 'login', :action => 'login' end def update_activity_time session[:expires_at] = 60.minutes.from_now end def update_activity_time from_now = 60.minutes.from_now if session[:expires_at].blank? session[:expires_at] = from_now else @time_left = (session[:expires_at].utc - Time.now.utc).to_i unless @time_left > 0 session[:expires_at] = from_now session_expiry end end end