ISSUES THAT DO NOT FOLLOW THIS TEMPLATE WILL BE CLOSED IMMEDIATELY.
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
ISSUES THAT DO NOT FOLLOW THIS TEMPLATE WILL BE CLOSED IMMEDIATELY.
StackOverflow.
guide
for instructions.
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".
authlogic/lib/authlogic/controller_adapters/rack_adapter.rb
Lines 5 to 40 in 8e3debe
I followed the steps described here, but it returns an error:
Replicable files: