Skip to content

Commit

Permalink
automatically add Devise/Warden middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Georgiy Melnikov authored and palkan committed Jun 29, 2024
1 parent 61e5d2f commit cfd1e55
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 67 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master

- Automatically add Warden::Manager to the AnyCable middleware stack when Devise is present. Remove the initializer generation

## 1.5.1 (2024-04-05)

- Add `anycable-rails-core.rb` to avoid adding `require: ["anycable-rails"]` to Gemfiles manually. ([@palkan][])
Expand Down Expand Up @@ -265,3 +267,4 @@ See [Changelog](https://github.com/anycable/anycable-rails/blob/0-6-stable/CHANG
[@DmitryTsepelev]: https://github.com/DmitryTsepelev
[@sponomarev]: https://github.com/sponomarev
[@bibendi]: https://github.com/bibendi
[@lHydra]: http://github.com/lHydra
14 changes: 11 additions & 3 deletions docs/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,22 @@ If your authentication method relies on non-standard Rack request properties (e.

Devise relies on [`warden`](https://github.com/wardencommunity/warden) Rack middleware to authenticate users.

In order to make it work with AnyCable, you must add this middleware to AnyCable's middleware stack like this:
By default, this middleware is automatically added to the AnyCable middleware stack when Devise is present.

You can edit `config/anycable.yml` to disable this behavior by changing the `use_warden_manager` parameter.

```yml
# config/anycable.yml
development:
use_warden_manager: false
```
And then, you can manually put this code, for example, into an initializer (`config/initializers/anycable.rb`) or any other configuration file.

```ruby
AnyCable::Rails::Rack.middleware.use Warden::Manager do |config|
Devise.warden_config = config
end
```

You can put this code, for example, into an initializer (`config/initializers/anycable.rb`) or any other configuration file.

Then, you can access the current user via `env["warden"].user(scope)` in your connection class (where `scope` is [Warden scope](https://github.com/wardencommunity/warden/wiki/Scopes), usually, `:user`).
3 changes: 2 additions & 1 deletion lib/anycable/rails/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
batch_broadcasts: false,
socket_id_header: "X-Socket-ID",
disable_rpc_pool_size_warning: false,
websocket_url: nil
websocket_url: nil,
use_warden_manager: true
)
AnyCable::Config.ignore_options :access_logs_disabled, :persistent_session_enabled
8 changes: 3 additions & 5 deletions lib/anycable/rails/rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ module Rails
# For instance, consider the Rails session middleware: it's responsible for restoring the
# session data from cookies.
#
# AnyCable adds session middelware by default to its own stack.
# AnyCable adds session middleware by default to its own stack.
#
# You can also use any Rack/Rails middleware you want. For example, to enable Devise/Warden
# You can also use any Rack/Rails middleware you want. For example, to enable CustomMiddleware
# you can add the following code to an initializer or any other configuration file:
#
# AnyCable::Rails::Rack.middleware.use Warden::Manager do |config|
# Devise.warden_config = config
# end
# AnyCable::Rails::Rack.middleware.use CustomMiddleware
module Rack
def self.app_build_lock
@app_build_lock
Expand Down
8 changes: 8 additions & 0 deletions lib/anycable/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ class Railtie < ::Rails::Railtie # :nodoc:
end
end

initializer "anycable.warden_manager" do
if defined?(::Devise) && AnyCable.config.use_warden_manager?
AnyCable::Rails::Rack.middleware.use Warden::Manager do |config|
::Devise.warden_config = config
end
end
end

# Since Rails 6.1
if respond_to?(:server)
server do
Expand Down
14 changes: 0 additions & 14 deletions lib/generators/anycable/setup/setup_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,6 @@ def development_method
end
end

def devise
return unless devise?

inside("config/initializers") do
template "anycable.rb"
end

say_status :info, "✅ config/initializers/anycable.rb with Devise configuration has been added"
end

def configs
inside("config") do
template "anycable.yml"
Expand Down Expand Up @@ -124,10 +114,6 @@ def rubocop?
!!gemfile_lock&.match?(/^\s+rubocop\b/)
end

def devise?
!!gemfile_lock&.match?(/^\s+devise\b/)
end

def local?
@devenv == "local"
end
Expand Down

This file was deleted.

31 changes: 0 additions & 31 deletions spec/generators/setup/setup_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,37 +173,6 @@
end
end

context "config/initializers/anycable.rb" do
subject do
run_generator default_opts
file("config/initializers/anycable.rb")
end

context "when no devise.rb" do
it "doesn't create anycable.rb initializer" do
expect(subject).not_to exist
end
end

context "when has devise.rb" do
before do
File.write(
File.join(destination_root, "Gemfile.lock"),
<<~CODE
GEM
specs:
devise
CODE
)
end

it "creates anycable.rb initializer" do
expect(subject)
.to contain("AnyCable::Rails::Rack.middleware.use Warden::Manager")
end
end
end

context "when RuboCop is present" do
before do
File.write(
Expand Down
13 changes: 13 additions & 0 deletions spec/lib/anycable/rails/railtie_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require "spec_helper"

describe AnyCable::Rails::Railtie do
describe "anycable.warden_manager" do
let(:initializer) { described_class.initializers.find { |init| init.name == "anycable.warden_manager" } }

it "includes warden manager initializer" do
expect(initializer).not_to be_nil
end
end
end

0 comments on commit cfd1e55

Please sign in to comment.