我有一個 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
這是我的習慣SessionController
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 => "User not ok, contact admin"但不知何故,它在重定向后沒有通過。
重要的一點,當它是純粹的設計錯誤時,我會收到一條訊息,例如Invalid Email or password..
uj5u.com熱心網友回復:
我知道了。這是一個中間件訂單問題。它必須按照以下順序
use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore
use ActionDispatch::Flash
use Warden::Manager
我已更新我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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/482392.html
