Rails 5带有前端引擎的API
我拥有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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我得到了它。这是一个中间件订单问题。必须使用以下
顺序
I got it. It was a middleware order issue. It has to be with the following order
I've update my
engine.rb
to include the middleware at the correct place