Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
4 tasks done
pyetrosafe opened this issue Oct 24, 2024 · 0 comments

Comments

@pyetrosafe
Copy link

pyetrosafe commented Oct 24, 2024

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

  • This is not a usage question.
    • Our volunteers' time is limited, so please ask usage questions on
      StackOverflow.
  • This is not a security issue.
  • This bug is reproducible with a clean install of authlogic
  • 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".

# Adapter for authlogic to make it function as a Rack middleware.
# First you'll have write your own Rack adapter where you have to set your cookie domain.
#
# class YourRackAdapter < Authlogic::ControllerAdapters::RackAdapter
# def cookie_domain
# 'your_cookie_domain_here.com'
# end
# end
#
# Next you need to set up a rack middleware like this:
#
# class AuthlogicMiddleware
# def initialize(app)
# @app = app
# end
#
# def call(env)
# YourRackAdapter.new(env)
# @app.call(env)
# end
# end
#
# And that is all! Now just load this middleware into rack:
#
# use AuthlogicMiddleware
#
# Authlogic will expect a User and a UserSession object to be present:
#
# class UserSession < Authlogic::Session::Base
# # Authlogic options go here
# end
#
# class User < ApplicationRecord
# acts_as_authentic
# end
#

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant