Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Rails 6.1, trouble with session management, sessions behave weirdly
I am at a complete dead end with Rails sessions.
I have an old Rails project. It started with Rails 3 or 4, and it is now running on Rails 6.1.7.10, the code has been updated carefully. I use sessions to keep track of logged-in users (just the user id and the expiry). The sessions have always been used without much config. My total amount of config is now (in config/initializer/session.rb):
Rails.application.config.session_store :cookie_store, key: '_qc24_session', domain: :all
and, since I use Devise (v 4.9.4, in vendor/mymodules/config/initializer/devise.rb):
config.skip_session_storage = [:http_auth]
I am running into several problems.
1. Increasingly, people need to sign in twice.
The first time somehow the session will not get written, although if I read from the session directly after writing into it, the value is written. What happens is:
- The user logs in, the session gets written according to the logs, the controller redirects to the user's start page.
- The controller method there tries to retrieve the session data, notices the session is empty and redirects the user to the login page.
- The user inputs their login data a second time, presses the login button again, and then the login succeeds.
2. All guides state that you read and write from/to sessions using symbolic keys, but I need to use string keys.
If I try using symbolic keys, the sessions will not work at all. I've posted on StackOverflow about that (https://stackoverflow.com/questions/79897707/rails-6-session-keys-are-strings-contrary-to-all-guides). I need to use string keys.
3. session_store :cache_store shows the same behaviour
The bit mentioned in Nr. 1 pointed to some kind of async problem, so I activated caching in dev.mode and changed the session store type to
Rails.application.config.session_store :cache_store, key: '_qc24_session', domain: :all
Then I get the same behaviour: The user needs to log in twice.
I read and write from/to sessions simply by using session['user_id'].
I can not find any sort of comprehensive information that allows me to begin debugging this situation. It always seems that all you do is state which kind of session_store you want, Rails does the rest. Obviously this is not true.
It is perfectly possible that owing to past migrations of Rails versions the project is not configured correctly, but how can I figure out what is wrong?

0 comment threads