How to I dynamically set the expiry time for a cookie-based session in Rails

前端 未结 6 611
失恋的感觉
失恋的感觉 2021-02-02 01:32

I\'m currently using the ActiveRecord-based session store for my Rails app and I have a background process which clears out inactive sessions every 30 minutes.

I\'d like

6条回答
  •  孤独总比滥情好
    2021-02-02 01:40

    Ideally, you'd want to add something like this to environment.rb:

    session :session_expires => 1.day.from_now
    

    But that won't work because the code is only run once when the APP is started and thus the next day all your sessions are being created with an expiration in the past.

    I usually set the session_expires to some time far in the future (6 months). Then manually set and check a session[:expires] date in a before_filter on my application controller and reset the session when that date has passed.

    This makes it VERY easy to add a 'Keep me logged in for ___' option when signing in, you just set session[:expires] = Time.now + ___

提交回复
热议问题