Ruby on Rails session expire date

3.8k Views Asked by At

I have following line in my application which let default cookie expire date 20 minutes.

Rails.application.config.session_store :cookie_store, key: '_rails-omniauth_session', expire_after: 20.minutes

Bu I want to set specific expire date for some reasons. I have tried following lines but they couldn't make it.

request.session_options[:expire_after] = 2.years
ActionController::Base.session_options[:expire_after] = 2.years

How can set custom expire date ?

Ang suggestions,

Thanks.

1

There are 1 best solutions below

0
On

If you need to set expiration period for sessions through all controllers in your application, simply add the following option to your config/intializers/session_store.rb file:

:expire_after => 60.minutes

If you need to set different expiration time in different controllers or actions, use the following code in action or some before_filter:

request.session_options = request.session_options.dup
request.session_options[:expire_after] = 5.minutes
request.session_options.freeze

Duplication of the hash is needed only because it is already frozen at that point

Source