Things work fine locally. User logs in -> user id is saved in rails session -> refresh browser -> still logged in as expected. However, on Heroku, the flow is broken. User logs in -> user id is saved in rails session -> refresh browser -> session variable :current_user_id is gone and they’re logged out.
It was set up as a standard rails app (not api only).
config.ru (also put this in cors.rb)
use Rack::Cors do
allow do
origins 'http://localhost:3000', 'https://client-side.herokuapp.com'
resource '*',
headers: :any,
methods: [:get, :post, :delete, :put, :options],
credentials: true
end
end
session_store.rb
if Rails.env === 'production'
Rails.application.config.session_store :cookie_store, :key => '_myapp', domain: 'app-client.herokuapp.com'
else
Rails.application.config.session_store :cookie_store, key: '_myapp'
end
application.rb
config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore
config.middleware.insert_after(ActionDispatch::Cookies, ActionDispatch::Session::CookieStore)
Copied the application.rb code snippet above from this post: Adding cookie session store back to Rails API app but it made no difference. Maybe because mine is a standard Rails app and theirs was an API only app.
Does anyone know what would cause the session variable key :current_user_id and it's value to not exist in the session object only in production mode (Heroku)? ie cookies are never written.