authlogic icon indicating copy to clipboard operation
authlogic copied to clipboard

Middleware with RackAdapter does not work: "uninitialized constant Authlogic::ControllerAdapters::RackAdapter"

Open pyetrosafe opened this issue 1 year ago • 0 comments

ISSUES THAT DO NOT FOLLOW THIS TEMPLATE WILL BE CLOSED IMMEDIATELY.

  • [x] This is not a usage question.
    • Our volunteers' time is limited, so please ask usage questions on StackOverflow.
  • [x] This is not a security issue.
  • [x] This bug is reproducible with a clean install of authlogic
  • [x] I am committed to fixing this in a reasonable amount of time, and responding promptly to feedback.

Expected Behavior

I'm trying to create a middleware to manage user permissions. To do this, I would like to use Authlogic and retrieve the logged-in user from the session, or if not, take action in this case.

Actual Behavior

I'm using the most recent version of Authlogic (Authlogic 6.4.3), Ruby 3.3.5, and Rails 7.1.4.

I couldn't find instructions on how to use Authlogic with Rack in any tutorial, but I found comments in the file "/lib/authlogic/controller_adapters/rack_adapter.rb".

https://github.com/binarylogic/authlogic/blob/8e3debe3db65290d4f161ae6ddff0443c34fd5d1/lib/authlogic/controller_adapters/rack_adapter.rb#L5-L40

I followed the steps described here, but it returns an error:

uninitialized constant Authlogic::ControllerAdapters::RackAdapter

Replicable files:

# config/application.rb

require_relative "boot"

require "rails/all"

require_relative '../app/middlewares/rules_permissions'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module OnlineCourses
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 7.1

    # Please, add to the `ignore` list any other `lib` subdirectories that do
    # not contain `.rb` files, or that should not be reloaded or eager loaded.
    # Common ones are `templates`, `generators`, or `middleware`, for example.
    config.autoload_lib(ignore: %w(assets tasks))

    # config.autoload_paths += %W(#{config.root}/lib)

    # Configuration for the application, engines, and railties goes here.
    #
    # These settings can be overridden in specific environments using the files
    # in config/environments, which are processed later.
    #
    # config.time_zone = "Central Time (US & Canada)"
    # config.eager_load_paths << Rails.root.join("extras")

    # config.time_zone = "America/Sao_Paulo"
    config.time_zone = ActiveSupport::TimeZone[Time.now.strftime('%z').gsub('0', '').to_i]
    config.active_record.default_timezone = :local

    config.assets.compile = true

    config.middleware.use RulesPermissions
  end
end
# app/middlewares/rules_permissions_rack_adapter.rb

class RulesPermissionsRackAdapter < Authlogic::ControllerAdapters::RackAdapter
  def cookie_domain
    # 'your_cookie_domain_here.com'
    '/'
  end
end
# app/middlewares/rules_permissions.rb

class RulesPermissions

  def initialize(app)
    @app = app
  end

  def call(env)
    RulesPermissionsRackAdapter.new(env)
    puts 'Play here';
    
    if UserSession.find
      # do something
    end
    
    @app.call(env)
  end

end
# app/models/user.rb

class User < ApplicationRecord
  acts_as_authentic do |c|
    c.crypto_provider = Authlogic::CryptoProviders::BCrypt
    c.require_password_confirmation = true
  end

  # Validate email, login, and password as you see fit.
  #
  # Authlogic < 5 added these validation for you, making them a little awkward
  # to change. In 4.4.0, those automatic validations were deprecated. See
  # https://github.com/binarylogic/authlogic/blob/master/doc/use_normal_rails_validation.md

  validates :email,
  format: {
    with: /@/,
    message: "should look like an email address."
  },
  length: { maximum: 100 },
  uniqueness: {
    case_sensitive: false,
    if: :will_save_change_to_email?
  }


  validates :password,
  confirmation: { if: :require_password? },
  length: {
    minimum: 8,
    if: :require_password?
  }

  validates :password_confirmation,
  length: {
    minimum: 8,
    if: :require_password?
  }
end
# app/models/user_session.rb

class UserSession < Authlogic::Session::Base
end

pyetrosafe avatar Oct 24 '24 04:10 pyetrosafe