Login Session Data Not Persisting in Rails 6

Rails 6 session variables not persisting

Ok, found the problem. Turns out that I had copied the setting config.session_store :cache_store in development.rb from a different project I had been working on. However, this setting was added as part of the StimulusReflex setup for that other project.

From the StimulusReflex docs:

Cookie-based session storage is not currently supported by StimulusReflex.

Instead, we enable caching in the development environment so that we can assign our user session data to be managed by the cache store.

The default setting for this option is cookie_store. By changing it to :cache_store without specifying a cache repo, it implements ActionDispatch::Session::CacheStore and defaults to storing it in Rails.cache, which uses the :file_store option, which dumps it in tmp/cache.

However, further down in development.rb, there is some conditional logic that assigns config.cache_store to :null_store if there is no caching-dev.txt file. This implements ActiveSupport::Cache::NullStore, which is "a cache store implementation which doesn't actually store anything."

So because I had not enabled caching with rails dev:cache for this project, the session cache was getting toasted with every request.

LESSON LEARNED: Be very careful when copying config settings from an old project to a new one!

Rails session not persisting after redirects

You can try this
pass book id to session instead of whole book object.

e.g.

in page A

book = Book.find(1)
session[:abc] = book.id

in page B

book = Book.find(session[:abc])

Rails 5 sessions not persisting

I found out what my problem was. In my application.html.erb, my logout link was incorrect and was forcing my user to logout immediately after logging them in.

I had <%= link_to "LOGOUT", logout %> when it should've been <%= link_to "LOGOUT", logout_path, method: :delete %>.

Rails - Session Variable Not Persisting Through Apartment Switch

Found the answer here: Share session (cookies) between subdomains in Rails?

The problem was in our session_store.rb. We weren't taking into account that by setting domain: :all, we were disregarding the default TLD length of 1. We were using lvh.me instead of localhost.

Hope this helps others!



Related Topics



Leave a reply



Submit