Implementation of “Remember me” in a Rails application

前端 未结 7 1026
旧时难觅i
旧时难觅i 2020-12-24 05:11

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 whet

相关标签:
7条回答
  • 2020-12-24 05:26

    The restful_authentication plugin has a good implementation of this:

    http://agilewebdevelopment.com/plugins/restful_authentication

    0 讨论(0)
  • 2020-12-24 05:29

    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.

    0 讨论(0)
  • 2020-12-24 05:29

    I would go for Devise for a brilliant authentication solution for rails.

    0 讨论(0)
  • 2020-12-24 05:31

    This worked like a charm for me:

    http://squarewheel.wordpress.com/2007/11/03/session-cookie-expiration-time-in-rails/

    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.

    Bascially, it's as simple as:

    1. including one file in vendor/plugins directory
    2. set session expiry value in application controller using just one line
    0 讨论(0)
  • 2020-12-24 05:35

    You should almost certainly not be extending the session cookie to be long lived.

    Although not dealing specifically with rails this article goes to some length to explain 'remember me' best practices.

    In summary though you should:

    • Add an extra column to the user table to accept a large random value
    • Set a long lived cookie on the client which combines the user id and the random value
    • When a new session starts, check for the existence of the id/value cookie and authenticate the new user if they match.

    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.

    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.

    0 讨论(0)
  • 2020-12-24 05:40

    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:

    railscasts #67 restful_authentication

    Here is a link to the plugin itself

    restful_authentication

    0 讨论(0)
提交回复
热议问题