Session token not saving Rails 6 production build

Viewed 471

I'm building a Rails 6.0.3.4 backend, React.js frontend app and for some reason my session token is not saving on the production build after working perfectly fine on the development build. I've loosened restrictions on the production build on Heroku (in session_store.rb I tweaked it from requiring a specific domain to all domains). When I log in I have it return the session token data and it is there but it's not saving anything (tried multiple browsers/computers). On my react front end I do have {withCredentials: true} in my login/sign up components.

session_store.rb

if Rails.env.production?
  Rails.application.config.session_store :cookie_store, key: '_10-athletes', domain: :all
else
  Rails.application.config.session_store :cookie_store, key: '_10-athletes'
end

In my sessions_controller.rb

def create
    @user = User.find_by(email: session_params[:usernameOrEmail])
    unless @user
      @user = User.find_by(username: session_params[:usernameOrEmail])
    end

    if @user && @user.authenticate(session_params[:password])
      login!
      render json: {
        logged_in: true,
        user: @user,#returns gthe user
        session: session, #returns appropriate session info: session_id: 32 digit hex value, user_id: 1
        production: Rails.env.production? #returns true, was curious if it was not registering properly

      }
    else
      # working fine
      }
    end
  end

def is_logged_in?
    if logged_in? && current_user
      # logged_in? is returning false, jumping to else
    else
      render json: {
        session: session[:user_id], #returns null
      }
    end
  end

application_controller.rb

def login!
    session[:user_id] = @user.id
  end
def logged_in?
    !!session[:user_id]
  end

config/application.rb

  class Application < Rails::Application
    config.load_defaults 6.0
    config.middleware.use ActionDispatch::Cookies
    config.middleware.use ActionDispatch::Session::CookieStore
    config.middleware.insert_after(ActionDispatch::Cookies, ActionDispatch::Session::CookieStore)
  end

config/environments/production.rb

Rails.application.configure do

  config.cache_classes = true
  config.eager_load = true
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true
  config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
  config.assets.compile = false
  config.active_storage.service = :local
  config.force_ssl = true # tested both including this line and not
  config.api_only = false #tested both including this line and not
  config.log_level = :debug
  config.log_tags = [ :request_id ]
  config.action_mailer.perform_caching = false
  config.i18n.fallbacks = true
  config.active_support.deprecation = :notify
  config.log_formatter = ::Logger::Formatter.new
  if ENV["RAILS_LOG_TO_STDOUT"].present?
    logger           = ActiveSupport::Logger.new(STDOUT)
    logger.formatter = config.log_formatter
    config.logger    = ActiveSupport::TaggedLogging.new(logger)
  config.active_record.dump_schema_after_migration = false
end

Edit: Added in production.rb

1 Answers

Do you have secure cookies enabled as a option? With SSL?

Related