简体   繁体   中英

Rails: Destroy session with a session_id

I'm writing a gem which needs to destroy arbitrary session by a session_id.

Currently I'm using the following codes

case Rails.application.config.session_store
when ActionDispatch::Session::CacheStore
  Rails.cache.delete("_session_id:#{session_id}")
when ActionDispatch::Session::ActiveRecordStore
  ActiveRecord::SessionStore.session_class.find(session_id).destroy
when ActionDispatch::Session::CookieStore
  # cannot remove a client-storage session
end

But I've found destroy_session(env, session_id, options) method for every SessionStorage class. How can I call this method in a script? (not in a request or any controller)

If you're storing session in the DB, can you try this in your console:

rake db:sessions:clear

You can also destroy the whole session with the following from your controller:

reset_session

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM