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

Fixing compatibility with Rails 7 and API mode #5474

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/devise_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# All Devise controllers are inherited from here.
class DeviseController < Devise.parent_controller.constantize
include Devise::Controllers::ScopedViews
include Devise::Controllers::Rails7ApiMode

if respond_to?(:helper)
helper DeviseHelper
Expand Down
1 change: 1 addition & 0 deletions lib/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Devise

module Controllers
autoload :Helpers, 'devise/controllers/helpers'
autoload :Rails7ApiMode, 'devise/controllers/rails7_api_mode'
autoload :Rememberable, 'devise/controllers/rememberable'
autoload :ScopedViews, 'devise/controllers/scoped_views'
autoload :SignInOut, 'devise/controllers/sign_in_out'
Expand Down
27 changes: 27 additions & 0 deletions lib/devise/controllers/rails7_api_mode.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module Devise
module Controllers
module Rails7ApiMode
extend ActiveSupport::Concern

class FakeRackSession < Hash
def enabled?
false
end
end

included do
before_action :set_fake_rack_session_for_devise

private

def set_fake_rack_session_for_devise
if Rails.configuration.respond_to?(:api_only) && Rails.configuration.api_only
request.env['rack.session'] ||= FakeRackSession.new
end
end
end
end
end
end
15 changes: 15 additions & 0 deletions test/controllers/rails7_api_mode_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require 'test_helper'

class Rails7ApiModeTest < Devise::ControllerTestCase
tests Devise::SessionsController
include Devise::Test::ControllerHelpers

test 'is injected into the Devise controllers' do
assert @controller.is_a?(Devise::Controllers::Rails7ApiMode)
end

# note: disabling API mode in one test seems not feasible; the test harness breaks a little bit.
# we need to understand better how to cover API-only mode in devise to prevent future issues.
end