It seems when a user logs out via standard Devise controllers, Devise destroys the entire session store, not just its own data. Is there any way to avoid this behavior? I have other irrelevant data that should be kept around.
session[:my_var] = "123"
Log out via devise...
puts session[:my_var]
# => nil
In the lasts versions of devise it is not necesary to override the sessions controller, instead you can just use:
In the
devise.rb
file to get the desired behaviour.The
destroy
¹ method ofSessionsController
contains the following line:The
sign_out_all_scopes
² method callswarden.logout
without any arguments, and thesign_out
³ method callswarden.logout(scope)
.The documentation of the
logout
⁴ method states:Conclusion:
sign_out
should preserve the session when given a specific scope. However, I don't see any way to do that.sign_out_all_scopes
is always called first, and will only returnfalse
if it couldn't log any user out.I recommend either posting a feature request on their issue tracker or developing your own authentication solution. Rails now provides
has_secure_password
, and these days people seem to be going for the latter in order to avoid running into these problems.¹
Devise::SessionsController#destroy
²
Devise::Controllers::Helpers#sign_out_all_scopes
³
Devise::Controllers::Helpers#sign_out
⁴
Warden::Proxy#logout
In addition to Mattheus. The statement
is perhaps the best general log out statement, considering the possibility of being signed in with multiple roles. If, for you case, your user is just signed in as one role, and you want to preserve the rest of the session on signout, the easiest way is to do:
Open app/controllers/devise/sessions_controller.rb in your editor. In the method destroy, replace
with
Save and exit editor and do
In the Gemfile of your project, describe the dependency to devise like
You could just override Devise's SessionController, like I did to preserve a shopping cart:
sessions_controller.rb
routes.rb