We've moved discussions to Discord

Sessions not persisting

Laetitia Massa
I'm recoding an existing e-commerce (built in Rails 4) with Jumpstart (Rails 6), and it seems that in Jumpstart, the session (on which depend current_order) does not persist between user requests, which breaks pretty much everything when the unconnected user is using it.

Here is my application_controller.rb 

  helper_method :current_order
  
  def current_order
    if !session[:order_id].nil?
      if Order.find(session[:order_id]).is_completed
        Order.new
      else
        Order.find(session[:order_id])
      end
    else
      Order.new
    end
  end

Here is what I found that might be the source of my problem, but don't know what I should do -
the app/application_cable/connection.rb file :

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    include SetCurrentRequestDetails

    identified_by :current_user, :current_account
    delegate :session, to: :request

    def connect
      self.current_user = find_verified_user
      set_request_details
      self.current_account = Current.account

      logger.add_tags "ActionCable", "User #{current_user.id}", "Account #{current_account.id}"
    end

    protected

    def find_verified_user
      if (current_user = env["warden"].user)
        current_user
      else
        reject_unauthorized_connection
      end
    end

    def user_signed_in?
      !!current_user
    end

    # Used by set_request_details
    def set_current_tenant(account)
      ActsAsTenant.current_tenant = account
    end
  end
end


My guess is that

delegate :session, to: :request

creates a new session each time the unconnected user makes a request?
If yes, how to fix it without breaking other part of the jumpstart app that might need it to work properly?

Many thanks!

Laetitia Massa
I'm recoding an existing e-commerce (built in Rails 4) with Jumpstart (Rails 6), and it seems that in Jumpstart, the session (on which depend current_order) does not persist between user requests, which breaks pretty much everything when the unconnected user is using it.

Here is my application_controller.rb 

  helper_method :current_order
  
  def current_order
    if !session[:order_id].nil?
      if Order.find(session[:order_id]).is_completed
        Order.new
      else
        Order.find(session[:order_id])
      end
    else
      Order.new
    end
  end

Here is what I found that might be the source of my problem, but don't know what I should do -
the app/application_cable/connection.rb file :

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    include SetCurrentRequestDetails

    identified_by :current_user, :current_account
    delegate :session, to: :request

    def connect
      self.current_user = find_verified_user
      set_request_details
      self.current_account = Current.account

      logger.add_tags "ActionCable", "User #{current_user.id}", "Account #{current_account.id}"
    end

    protected

    def find_verified_user
      if (current_user = env["warden"].user)
        current_user
      else
        reject_unauthorized_connection
      end
    end

    def user_signed_in?
      !!current_user
    end

    # Used by set_request_details
    def set_current_tenant(account)
      ActsAsTenant.current_tenant = account
    end
  end
end


My guess is that

delegate :session, to: :request

creates a new session each time the unconnected user makes a request?
If yes, how to fix it without breaking other part of the jumpstart app that might need it to work properly?

Many thanks!

Notifications
You’re not receiving notifications from this thread.
© 2022 Jumpstart Pro by GoRails, LLC