user2340939
user2340939

Reputation: 1981

Sign out of Devise from console

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).

Upvotes: 1

Views: 3388

Answers (3)

John Baker
John Baker

Reputation: 2398

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: "")

Upvotes: 0

lacostenycoder
lacostenycoder

Reputation: 11216

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

Logout users with devise gem rails

Upvotes: 1

Austio
Austio

Reputation: 6085

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.

Upvotes: 2

Related Questions