简体   繁体   English

带有前端引擎的 Rails 5 API

[英]Rails 5 API with front-end engine

I've a Rails 5 API with devise/doorkeeper perfectly working.我有一个 Rails 5 API 和设计/门卫完美地工作。

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这是我的自定义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我得到了这个自定义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. env[warden.options]充满了:message => "User not ok, contact admin"但不知何故,它在重定向后没有通过。

Important point, I got flash message when it's a pure devise error like Invalid Email or password.重要的一点,当它是一个纯粹的设计错误(如Invalid Email or password. . .

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我已经更新了我的engine.rb以在正确的位置包含中间件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM