Our Rails app is using Restful Authentication for user/session management and it seems that logging in to the same account from multiple computers kills the session on the other computers, thus killing the “Remember me” feature.
So say I’m at home and log in to the app (and check “Remember me”). Then I go to the office and log in (and also check “Remember me”). Then, when I return home, I return to the app and and have to re-log in.
How can I allow logging in from multiple machines and keep the “Remember me” functionality working across them all?
You are going to sacrifice some security by doing this, but it’s definitely possible. There are two ways you should be able to accomplish this.
In the first, you can override the make_token method in your user model. The model is currently implemented as follows.
Every time a user logs in, with or without a cookie, the
make_tokenmethod is called which generates and saves a newremember_tokenfor the user. If you had some other value that was unique to the user that couldn’t be guessed, you could replace themake_tokenmethod.This would ensure that the token never changes, but it would also enable anyone that got the token to impersonate the user.
Other than this, if you take a look at the
handle_remember_cookie!method in theauthenticated_system.rbfile, you should be able to change this method to work for you.You’ll notice that this method calls three methods in the user model,
refresh_token,remember_me, andforget_me.All three of these methods reset the token.
forget_mesets it tonil, whereas the other two set it to the value returned bymake_token. You can override these methods in the user model, to prevent them from resetting the token if it exists and isn’t expired. That is probably the best approach, or you could add some additional logic to thehandle_remember_cookie!method, though that would likely be more work.If I were you, I would override
remember_me_until,forget_me, andrefresh_tokenin the user model. The following should work.Note that by doing this, you’re taking out the features that protect you from token stealing. But that’s a cost benefit decision you can make.