How do I manually sign out a user (or all users) from the Rails console? I'm using Devise gem. None of the solutions I've found work for me. How can I call sign_out on a user? I've tried e.g. Devise::Controllers::SignInOut#sign_out(User.first)
.

- 1,791
- 2
- 17
- 44
-
2How did you sign in an user from the console? – Raj Nov 22 '18 at 04:45
-
I think this will help you for answer, [devise logout functionality](https://stackoverflow.com/questions/10153040/stop-devise-from-clearing-session/10157046#10157046) – ray Nov 22 '18 at 05:37
3 Answers
A lot of this is going to be heavily dependent on how you are handling sessions and if you are backing those authentications with some database persistence (Sessions or Authentications table is normally where that would exists). If it is just by setting a cookie, you won't be able to do that directly through the console because it is related to expiring a cookie through warden in the browser.

- 5,939
- 20
- 34
Delete all users sessions is easy if you store user sessions in sessions table which is pretty standard. This is very heavy handed.
sql = 'DELETE FROM sessions;' # will destroy all sessions in the database
ActiveRecord::Base.connection.execute(sql)
If you're using Devise gem, for destroy single user session have a look here

- 10,623
- 4
- 31
- 48
To sign out you can use this command, which will clear all user sessions:
rake db:sessions:clear
If you are using only one session per user account, you can use this:
user = User.first
user.update_attributes(unique_session_id: "")

- 2,315
- 13
- 12
-
unknown attribute 'unique_session_id' for User. Doesn't look to be standard field – iCyborg Mar 14 '23 at 19:45