0.0
There's a lot of open issues
Encrypted, database-backed session store for Rails.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 8.0.0
>= 8.0.0
>= 8.0.0
>= 1.15.0
>= 8.0.0
~> 2.6
 Project Readme

Stored Session

Gem Version Required Ruby Version Required Rails Version CI Maintainability codecov License: MIT

Encrypted, database-backed session store for Rails. It is a modernization of the activerecord-session_store gem that was previously extracted from Rails. Stored Session is encrypted by default and is tested with MySQL, PostgreSQL, and SQLite against Rails 8+.

Features

Should I use this?

Probably not.

The original active_record_store was extracted from Rails 4 in 2012 and moved to its own gem.

Rails 4 also began encrypting and signing cookies by default, including the session cookie. Prior to Rails 4, active_record_store was recommended when sensitive data needed to be stored in the session. Today, the secure defaults in Rails remove the need to store the session server-side for that reason.

Additionally, database-backed sessions put an increasing amount of strain on the database as traffic grows due to the sessions table being constantly operated against.

I built Stored Session primarily as an exercise in creating an "omakase" Rails engine to learn the framework's common patterns for things like engine configuration, instrumentation, and testing against multiple databases. Before using this gem in production, please make sure you really have a need for database-backed session storage!

When might you actually need this?

You might consider using database-backed sessions if you:

  • Have regulatory requirements mandating server-side session storage
  • Need to store session data larger than the 4KB cookie limit
  • Require detailed analytics or audit trails for every session

If none of these apply to your application, you're likely better off using Rails' default cookie-based session store.

Prerequisites

  • Ruby >= 3.2.0
  • Rails >= 8.0.0

Installation

Add Stored Session to your application by following these steps:

  1. bundle add stored_session
  2. bin/rails stored_session:install:migrations
  3. bin/rails db:migrate

ActiveRecord encryption must be enabled in order to use Stored Session. Follow the instructions in the guide to configure.

Then, set your session store in config/initializers/session_store.rb:

Rails.application.config.session_store :stored_session_store, key: '_my_app_session`

When Solid Queue is used as your ActiveJob queue adapter, add StoredSession::ExpireSessionsJob to config/recurring.yml:

production:
  expire_sessions:
    class: "StoredSession::ExpireSessionsJob"
    schedule: every day

Instrumentation

Stored Session instruments session store operations with ActiveSupport::Notifications:

session_read.stored_session

Key Value
:sid The hashed session ID
{
  sid: '2::350cabf53a661de4fcf3d0ba6c6c65fd560b41e9697cf000168a9f420fb5366a'
}

session_write.stored_session

Key Value
:sid The hashed session ID
{
  sid: '2::350cabf53a661de4fcf3d0ba6c6c65fd560b41e9697cf000168a9f420fb5366a'
}

session_delete.stored_session

Key Value
:sid The hashed session ID
{
  sid: '2::350cabf53a661de4fcf3d0ba6c6c65fd560b41e9697cf000168a9f420fb5366a'
}

Acknowledgements

This gem builds upon the excellent work of many contributors in the Ruby on Rails ecosystem. Special thanks to:

  • The Rails core team and contributors, whose test suites and session store implementations in Rails itself core provided a robust foundation.
  • The maintainers and contributors of the original activerecord-session_store gem, whose longstanding work influenced this implementation.
  • The Solid Cache and Solid Queue maintainers and contributors, particularly for their modern database interaction patterns.

Portions of the gem boilerplate, implementation, and test suite and gem infrastructure were adapted from these projects, each of which are also distributed under the MIT License.

License

The gem is available as open source under the terms of the MIT License.