<p>My Rails-app has a sign in box with a "remember me" checkbox. Users who check that box should remain logged in even after closing their browser. I'm keeping track of whether users are logged in by storing their id in the user's session. </p>

<p>But sessions are implemented in Rails as session cookies, which are not persistent. I can <em>make</em> them persistent:</p>

<pre><code>class ApplicationController &lt; ActionController::Base
  before_filter :update_session_expiration_date

  private

  def update_session_expiration_date
    options = ActionController::Base.session_options
    unless options[:session_expires]
      options[:session_expires] = 1.year.from_now
    end
  end
end
</code></pre>

<p>But that seems like a hack, which is surprising for such common functionality. Is there any better way?</p>

<p><strong>Edit</strong></p>

<p>Gareth's answer is pretty good, but I would still like an answer from someone familiar with Rails 2 (because of it's unique <code>CookieSessionStore</code>).</p>

[{'answer_id': 483, 'author': 'Gareth Simpson', 'author_id': 147, 'author_profile': 'https://Stackoverflow.com/users/147', 'pm_score': 5, 'selected': False, 'text': '<p>You should almost certainly not be extending the session cookie to be long lived.</p>\n\n<p>Although not dealing specifically with rails <a href="http://fishbowl.pastiche.org/2004/01/19/persistent_login_cookie_best_practice" rel="noreferrer">this article</a> goes to some length to explain \'remember me\' best practices.</p>\n\n<p>In summary though you should:</p>\n\n<ul>\n<li>Add an extra column to the user table to accept a large random value</li>\n<li>Set a long lived cookie on the client which combines the user id and the random value</li>\n<li>When a new session starts, check for the existence of the id/value cookie and authenticate the new user if they match.</li>\n</ul>\n\n<p>The author also recommends invalidating the random value and resetting the cookie at every login.  Personally I don\'t like that as you then can\'t stay logged into a site on two computers.  I would tend to make sure my password changing function also reset the random value thus locking out sessions on other machines. </p>\n\n<p>As a final note, the advice he gives on making certain functions (password change/email change etc) unavailable to auto authenticated sessions is well worth following but rarely seen in the real world.</p>\n'}, {'answer_id': 4025, 'author': 'Jarin Udom', 'author_id': 574, 'author_profile': 'https://Stackoverflow.com/users/574', 'pm_score': 3, 'selected': False, 'text': '<p>The restful_authentication plugin has a good implementation of this: </p>\n\n<p><a href="http://agilewebdevelopment.com/plugins/restful_authentication" rel="noreferrer">http://agilewebdevelopment.com/plugins/restful_authentication</a></p>\n'}, {'answer_id': 38481, 'author': 'Teflon Ted', 'author_id': 4061, 'author_profile': 'https://Stackoverflow.com/users/4061', 'pm_score': 3, 'selected': False, 'text': "<p>Note that you don't want to persist their session, just their identity.  You'll create a fresh session for them when they return to your site.  Generally you just assign a GUID to the user, write that to their cookie, then use it to look them up when they come back.  Don't use their login name or user ID for the token as it could easily be guessed and allow crafty visitors to hijack other users' accounts.</p>\n"}, {'answer_id': 77397, 'author': 'Daniel Beardsley', 'author_id': 13216, 'author_profile': 'https://Stackoverflow.com/users/13216', 'pm_score': 5, 'selected': True, 'text': '<p>I have spent a while thinking about this and came to some conclusions.  Rails session cookies are tamper-proof by default, so you really don\'t have to worry about a cookie being modified on the client end.</p>\n\n<p>Here is what I\'ve done:</p>\n\n<ul>\n<li>Session cookie is set to be long-lived (6 months or so)</li>\n<li>Inside the session store\n\n<ul>\n<li>An \'expires on\' date that is set to login + 24 hours</li>\n<li>user id</li>\n<li>Authenticated = true  so I can allow for anonymous user sesssions (not dangerous because of the cookie tamper protection)</li>\n</ul></li>\n<li>I add a before_filter in the Application Controller that checks the \'expires on\' part of the session.</li>\n</ul>\n\n<p>When the user checks the "Remember Me" box, I just set the session[:expireson] date to be login + 2 weeks.  No one can steal the cookie and stay logged in forever or masquerade as another user because the rails session cookie is tamper-proof.</p>\n'}, {'answer_id': 83714, 'author': 'Mattew', 'author_id': 14787, 'author_profile': 'https://Stackoverflow.com/users/14787', 'pm_score': 4, 'selected': False, 'text': '<p>I would suggest that you either take a look at the RESTful_Authentication plug in, which has an implementation of this, or just switch your implementation to use the RESTful Authentication_plugin.  There is a good explanation about how to use this plug in at Railscasts:</p>\n\n<p><a href="http://railscasts.com/episodes/67-restful-authentication" rel="noreferrer">railscasts #67 restful_authentication</a></p>\n\n<p>Here is a link to the plugin itself</p>\n\n<p><a href="http://rubygems.org/gems/restful_authentication" rel="noreferrer">restful_authentication</a></p>\n'}, {'answer_id': 107677, 'author': 'Allan L.', 'author_id': 19527, 'author_profile': 'https://Stackoverflow.com/users/19527', 'pm_score': 2, 'selected': False, 'text': '<p>This worked like a charm for me:</p>\n\n<p><a href="http://squarewheel.wordpress.com/2007/11/03/session-cookie-expiration-time-in-rails/" rel="nofollow noreferrer">http://squarewheel.wordpress.com/2007/11/03/session-cookie-expiration-time-in-rails/</a></p>\n\n<p>Now my CookieStore sessions expire after two weeks, whereby the user must submit their login credentials again in order to be persistently logged-in for another two weeks.</p>\n\n<p>Bascially, it\'s as simple as:</p>\n\n<ol>\n<li>including one file in vendor/plugins directory</li>\n<li>set session expiry value in application controller using just one line</li>\n</ol>\n'}, {'answer_id': 4703796, 'author': 'Ved', 'author_id': 389197, 'author_profile': 'https://Stackoverflow.com/users/389197', 'pm_score': 2, 'selected': False, 'text': '<p>I would go for Devise for a brilliant authentication solution for rails.</p>\n'}]
['https://Stackoverflow.com/questions/438', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/136/']