26

At some mystery point X with this rails app hosted on heroku, a logged in user would suddenly be logged in as another user. I am using the devise gem for authentication.

This has occurred for 2 users that we know of. I am currently tracking down what the root cause of this issue could be.

What I need to do right now is invalidate all devise sessions in order to force users to login again. After a user logs in, the problem seems to go away.

I tried reseting my secret_token but I was not forced to login again. I then scaled my web dynos down and then back up. I also restarted the app. All trying to get the secret_token change to reset the sessions.

Any other ideas?

JB.
  • 645
  • 1
  • 6
  • 10

9 Answers9

26

You should be able to change your session cookie name to invalidate all sessions, which lives in config/initializers/session_store.rb

YourApp::Application.config.session_store :cookie_store, key: '_change_me_session'
djcp
  • 699
  • 1
  • 6
  • 6
18

Changing your session_token will work if you're storing your sessions in cookies (default).

But if you're storing in active_record, then you can delete all the sessions by:

rake db:sessions:clear

then: BAM! no more sessions.

Jesse Wolgamott
  • 40,197
  • 4
  • 83
  • 109
6

Update on the accepted answer, now it is

rake tmp:clear

rake -T ... rake tmp:create # Creates tmp directories for sessions, cache, sockets, and pids

lab419
  • 1,129
  • 13
  • 16
  • Thank you! This is the answer that worked for me. I'm on Rails 4.2 with Devise and `db:sessions:clear` is not available as a task. It took about 5 minutes, but `tmp:clear` did it. – jlleblanc Jan 31 '19 at 14:11
  • This is the only one that worked for me. Changing cookie session key did not work – Jeff Davenport Feb 18 '20 at 22:37
4

If your sessions don't store any other critical information, you could clear the sessions:

rake db:sessions:clear
Matchu
  • 83,922
  • 18
  • 153
  • 160
1

Devise has a thing called timeoutable can you work with that?

catsby
  • 11,276
  • 3
  • 37
  • 37
1

Check out

  module ClassMethods
    Devise::Models.config(self, :timeout_in)
  end

I'm just guessing that you could do something like:

User.all.each do |user|
  user.timeout_in 1.second
end

But I'm not sure if this only manages new sessions.. and not existing ones?

Actually this is overly complex.. just try:

User.all.each do |user|
  sign_out user
end

See this post Log out all user with Devise

to do something like this from the console you will need to check out this example and adjust it for your needs

How to sign in a user using Devise from a Rails console?

Community
  • 1
  • 1
Abram
  • 39,950
  • 26
  • 134
  • 184
0

sign_out_all_scopes(lock = true) ⇒ Object

Sign out all active users or scopes. This helper is useful for signing out all roles in one click. This signs out ALL scopes in warden. Returns true if there was at least one logout and false if there was no user logged in on all scopes.

source: http://www.rubydoc.info/github/plataformatec/devise/Devise/Controllers/SignInOut

Community
  • 1
  • 1
plombix
  • 396
  • 3
  • 13
0

When cookie store is used, we have to regenerate the app secret_token which is used to encrypt the cookies.

file to configure secret_token: config/initializers/secret_token.rb

bundle exec rake secret Can be used to generate a new secret token.

https://www.tigraine.at/2012/08/03/how-to-expire-all-active-sessions-in-rails-3

Arjun
  • 577
  • 6
  • 13
0

Try this on your controller actions.

  def index
    reset_session
  end
Jin Lim
  • 1,759
  • 20
  • 24