Rails 5带有前端引擎的API

发布于 2025-01-31 20:45:46 字数 2513 浏览 3 评论 0原文

我拥有Devise/Doorkeeper完美工作的Rails 5 API。

我构建了一个具有所有设计视图的前端零件的引擎。除非守望者丢下错误时的闪光灯,否则一切都起作用。我已经手动缺少模块:

# app/controllers/concerns/api_to_web_controller.rb
module ApiToWebController
  extend ActiveSupport::Concern

  included do
    include ActionController::Helpers
    include ActionController::MimeResponds
    include ActionController::Redirecting
    include ActionView::Layouts
    include ActionController::EtagWithFlash
    include ActionController::Flash
    
    respond_to :html
  end
end


# app/controllers/concerns/devise_api_to_web.rb
##
# Module to integrate Devise:
#  - helpers
#  - layout
# It also redefine `redirect_to`.
# See https://github.com/heartcombo/responders/issues/222
# for more details
module DeviseApiToWeb
  extend ActiveSupport::Concern

  included do
    layout 'secret_migration/devise'

    if respond_to?(:helper_method)
      helpers = %w[resource scope_name resource_name signed_in_resource
                   resource_class resource_params devise_mapping]
      helper_method(*helpers)
    end

    def redirect_to(options = {}, response_options = {})
      super
    end
  end
end

这是我的自定义session controller

module MyEngine
  module V1
    # Override Devise::SessionsController
    class Users::SessionsController < Devise::SessionsController
      include ::ApiToWebController
      include ::DeviseApiToWeb
    end
  end
end

我得到了此自定义after_authentication

# config/initializers/warden.rb
Warden::Manager.after_authentication do |user, auth, _opts|
  next if user.is_ok?

  throw(:warden, :message => "User not ok, contact admin")
end

应用程序文件

module MyApp
  class Application < 
opts = { key: '_my_app', domain: 'example.com', tld_length: 1 }
Rails::ApplicationRails.application.config.session_store :disabled
  config.session_store :cookie_store, opts
  config.middleware.use ActionDispatch::Session::CookieStore, config.session_options
  config.middleware.insert_after(ActionDispatch::Cookies, ActionDispatch::Session::CookieStore,
                                 opts)

最后,我能够跟踪数据的 。 env [warden.options]充满了:message =&gt; “用户不正常,请联系管理”,但是以某种方式,它在重定向后没有通过。

重要的一点是,当它是纯设计错误时,我收到了Flash消息,例如无效的电子邮件或密码。

I've a Rails 5 API with devise/doorkeeper perfectly working.

I've built an engine having a front-end part with all devise views. Everything works unless the flash when warden is throwing an error. I've imported manually missing modules :

# app/controllers/concerns/api_to_web_controller.rb
module ApiToWebController
  extend ActiveSupport::Concern

  included do
    include ActionController::Helpers
    include ActionController::MimeResponds
    include ActionController::Redirecting
    include ActionView::Layouts
    include ActionController::EtagWithFlash
    include ActionController::Flash
    
    respond_to :html
  end
end


# app/controllers/concerns/devise_api_to_web.rb
##
# Module to integrate Devise:
#  - helpers
#  - layout
# It also redefine `redirect_to`.
# See https://github.com/heartcombo/responders/issues/222
# for more details
module DeviseApiToWeb
  extend ActiveSupport::Concern

  included do
    layout 'secret_migration/devise'

    if respond_to?(:helper_method)
      helpers = %w[resource scope_name resource_name signed_in_resource
                   resource_class resource_params devise_mapping]
      helper_method(*helpers)
    end

    def redirect_to(options = {}, response_options = {})
      super
    end
  end
end

Here is my custom SessionController

module MyEngine
  module V1
    # Override Devise::SessionsController
    class Users::SessionsController < Devise::SessionsController
      include ::ApiToWebController
      include ::DeviseApiToWeb
    end
  end
end

I got this custom after_authentication

# config/initializers/warden.rb
Warden::Manager.after_authentication do |user, auth, _opts|
  next if user.is_ok?

  throw(:warden, :message => "User not ok, contact admin")
end

Finally, the application file

module MyApp
  class Application < 
opts = { key: '_my_app', domain: 'example.com', tld_length: 1 }
Rails::ApplicationRails.application.config.session_store :disabled
  config.session_store :cookie_store, opts
  config.middleware.use ActionDispatch::Session::CookieStore, config.session_options
  config.middleware.insert_after(ActionDispatch::Cookies, ActionDispatch::Session::CookieStore,
                                 opts)

I've been able to track the data. env[warden.options] is well filled with :message => "User not ok, contact admin" but somehow, it's not passed after the redirection.

Important point, I got flash message when it's a pure devise error like Invalid Email or password..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

物价感观 2025-02-07 20:45:46

我得到了它。这是一个中间件订单问题。必须使用以下

use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore
use ActionDispatch::Flash
use Warden::Manager

顺序

module MyEngine
  ##
  # Engine initializers
  class Engine < ::Rails::Engine
    isolate_namespace MyEngine
    initializer 'use action dispatch flash' do |app|
      app.config.middleware.insert_after(ActionDispatch::Session::CookieStore, ActionDispatch::Flash)
    
      app.config.middleware.use Rack::MethodOverride
    end
  end
end
# config/application.rb
module MyApp
  class Application < Rails::Application
    # ...
    config.middleware.insert_before(Warden::Manager, ActionDispatch::Cookies)
    # ...
  end
end
# config/environments/development.rb
Rails.application.configure do
  opts = { key: '_my_app', domain: 'lvh.me', tld_length: 2 }
  Rails.application.config.session_store :disabled
  config.session_store :cookie_store, opts
  config.middleware.insert_after(ActionDispatch::Cookies, ActionDispatch::Session::CookieStore,
                                 opts)
end

I got it. It was a middleware order issue. It has to be with the following order

use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore
use ActionDispatch::Flash
use Warden::Manager

I've update my engine.rb to include the middleware at the correct place

module MyEngine
  ##
  # Engine initializers
  class Engine < ::Rails::Engine
    isolate_namespace MyEngine
    initializer 'use action dispatch flash' do |app|
      app.config.middleware.insert_after(ActionDispatch::Session::CookieStore, ActionDispatch::Flash)
    
      app.config.middleware.use Rack::MethodOverride
    end
  end
end
# config/application.rb
module MyApp
  class Application < Rails::Application
    # ...
    config.middleware.insert_before(Warden::Manager, ActionDispatch::Cookies)
    # ...
  end
end
# config/environments/development.rb
Rails.application.configure do
  opts = { key: '_my_app', domain: 'lvh.me', tld_length: 2 }
  Rails.application.config.session_store :disabled
  config.session_store :cookie_store, opts
  config.middleware.insert_after(ActionDispatch::Cookies, ActionDispatch::Session::CookieStore,
                                 opts)
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文