devise_token_auth Rails 5 API 和 Omniauth 的问题 - 在 process_action 回调之前:set_user_by_token 尚未定义

Issues with devise_token_auth Rails 5 API and Omniauth - Before process_action callback :set_user_by_token has not been defined

我正在为移动应用程序的 API 后端使用设置 devise_token_auth。

我正在使用:

电子邮件作为提供商一切正常

为了在客户端正确实现 Facebook 登录,并且仍然能够在服务器上进行身份验证,我按照 omniauth-gem:

中指示的方法进行操作

https://github.com/mkdynamic/omniauth-facebook 请参阅部分:使用 Facebook Javascript SDK

的客户端流程

这基本上是说要处理客户端中的所有登录,一旦用户登录,将客户端重定向到 API 回调 URL auth/facebook/callback(或 omniauth/facebook/callback 取决于你的路由)并让 Omniauth 解析签名的 cookie 和代码参数。

由于我使用的是 devise_token_auth,此时 gem 应该生成一个令牌并使用 provider:facebook 而不是 provider:email 登录。

首先我不得不在中间件中重新打开会话和闪存,因为 omniauth 出于某种原因需要它们(Rails 5 api-only 模式删除了它们)。

但现在我的问题是调用 GET auth/facebook/callback(或 omniauth/facebook/callback)时出现此错误,无论是否存在任何 parameters/cookies:

**ArgumentError (Before process_action callback :set_user_by_token has not been defined)**



Full stack trace follows.
Any help would be appreciated.

Started GET "/auth/facebook/callback" for 127.0.0.1 at 2016-05-30 14:43:47 -0500
ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"

ArgumentError (Before process_action callback :set_user_by_token has not been defined):

activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:641:in block (2 levels) in skip_callback' activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:637:ineach'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:637:in block in skip_callback' activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:568:inblock in update_callbacks'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:566:in reverse_each' activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:566:in__update_callbacks'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:636:in skip_callback' actionpack (5.0.0.rc1) lib/abstract_controller/callbacks.rb:210:inblock (3 levels) in module:ClassMethods'
actionpack (5.0.0.rc1) lib/abstract_controller/callbacks.rb:93:in block in _insert_callbacks' actionpack (5.0.0.rc1) lib/abstract_controller/callbacks.rb:92:ineach'
actionpack (5.0.0.rc1) lib/abstract_controller/callbacks.rb:92:in _insert_callbacks' actionpack (5.0.0.rc1) lib/abstract_controller/callbacks.rb:209:inblock (2 levels) in module:ClassMethods'
devise_token_auth (0.1.37) app/controllers/devise_token_auth/omniauth_callbacks_controller.rb:5:in <class:OmniauthCallbacksController>' devise_token_auth (0.1.37) app/controllers/devise_token_auth/omniauth_callbacks_controller.rb:2:inmodule:DeviseTokenAuth'
devise_token_auth (0.1.37) app/controllers/devise_token_auth/omniauth_callbacks_controller.rb:1:in <top (required)>' activesupport (5.0.0.rc1) lib/active_support/dependencies.rb:477:inload'
activesupport (5.0.0.rc1) lib/active_support/dependencies.rb:477:in block in load_file' activesupport (5.0.0.rc1) lib/active_support/dependencies.rb:662:innew_constants_in'
activesupport (5.0.0.rc1) lib/active_support/dependencies.rb:476:in load_file' activesupport (5.0.0.rc1) lib/active_support/dependencies.rb:375:inblock in require_or_load'
activesupport (5.0.0.rc1) lib/active_support/dependencies.rb:37:in block in load_interlock' activesupport (5.0.0.rc1) lib/active_support/dependencies/interlock.rb:12:inblock in loading'
activesupport (5.0.0.rc1) lib/active_support/concurrency/share_lock.rb:117:in exclusive' activesupport (5.0.0.rc1) lib/active_support/dependencies/interlock.rb:11:inloading'
activesupport (5.0.0.rc1) lib/active_support/dependencies.rb:37:in load_interlock' activesupport (5.0.0.rc1) lib/active_support/dependencies.rb:358:inrequire_or_load'
activesupport (5.0.0.rc1) lib/active_support/dependencies.rb:511:in load_missing_constant' activesupport (5.0.0.rc1) lib/active_support/dependencies.rb:203:inconst_missing'
activesupport (5.0.0.rc1) lib/active_support/inflector/methods.rb:270:in const_get' activesupport (5.0.0.rc1) lib/active_support/inflector/methods.rb:270:inblock in constantize'
activesupport (5.0.0.rc1) lib/active_support/inflector/methods.rb:266:in each' activesupport (5.0.0.rc1) lib/active_support/inflector/methods.rb:266:ininject'
activesupport (5.0.0.rc1) lib/active_support/inflector/methods.rb:266:in constantize' activesupport (5.0.0.rc1) lib/active_support/dependencies.rb:583:inget'
activesupport (5.0.0.rc1) lib/active_support/dependencies.rb:614:in constantize' actionpack (5.0.0.rc1) lib/action_dispatch/http/request.rb:93:incontroller_class'
actionpack (5.0.0.rc1) lib/action_dispatch/routing/route_set.rb:44:in controller' actionpack (5.0.0.rc1) lib/action_dispatch/routing/route_set.rb:30:inserve'
actionpack (5.0.0.rc1) lib/action_dispatch/routing/mapper.rb:16:in block in <class:Constraints>' actionpack (5.0.0.rc1) lib/action_dispatch/routing/mapper.rb:46:inserve'
actionpack (5.0.0.rc1) lib/action_dispatch/journey/router.rb:39:in block in serve' actionpack (5.0.0.rc1) lib/action_dispatch/journey/router.rb:26:ineach'
actionpack (5.0.0.rc1) lib/action_dispatch/journey/router.rb:26:in serve' actionpack (5.0.0.rc1) lib/action_dispatch/routing/route_set.rb:725:incall'
actionview (5.0.0.rc1) lib/action_view/digestor.rb:12:in call' omniauth (1.3.1) lib/omniauth/strategy.rb:186:incall!'
omniauth (1.3.1) lib/omniauth/strategy.rb:164:in call' omniauth (1.3.1) lib/omniauth/strategy.rb:186:incall!'
omniauth (1.3.1) lib/omniauth/strategy.rb:164:in call' omniauth (1.3.1) lib/omniauth/builder.rb:63:incall'
rack-attack (4.4.1) lib/rack/attack.rb:100:in call' rack (2.0.0.rc1) lib/rack/session/abstract/id.rb:222:incontext'
rack (2.0.0.rc1) lib/rack/session/abstract/id.rb:216:in call' actionpack (5.0.0.rc1) lib/action_dispatch/middleware/cookies.rb:613:incall'
warden (1.2.6) lib/warden/manager.rb:35:in block in call' warden (1.2.6) lib/warden/manager.rb:34:incatch'
warden (1.2.6) lib/warden/manager.rb:34:in call' rack (2.0.0.rc1) lib/rack/etag.rb:25:incall'
rack (2.0.0.rc1) lib/rack/conditional_get.rb:25:in call' rack (2.0.0.rc1) lib/rack/head.rb:12:incall'
activerecord (5.0.0.rc1) lib/active_record/migration.rb:552:in call' actionpack (5.0.0.rc1) lib/action_dispatch/middleware/callbacks.rb:38:inblock in call'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:97:in `__run_callbacks'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:750:in _run_call_callbacks' activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:90:inrun_callbacks'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/callbacks.rb:36:in call' actionpack (5.0.0.rc1) lib/action_dispatch/middleware/executor.rb:12:incall'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/remote_ip.rb:79:in call' actionpack (5.0.0.rc1) lib/action_dispatch/middleware/debug_exceptions.rb:49:incall'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/show_exceptions.rb:31:in call' railties (5.0.0.rc1) lib/rails/rack/logger.rb:36:incall_app'
railties (5.0.0.rc1) lib/rails/rack/logger.rb:24:in block in call' activesupport (5.0.0.rc1) lib/active_support/tagged_logging.rb:70:inblock in tagged'
activesupport (5.0.0.rc1) lib/active_support/tagged_logging.rb:26:in tagged' activesupport (5.0.0.rc1) lib/active_support/tagged_logging.rb:70:intagged'
railties (5.0.0.rc1) lib/rails/rack/logger.rb:24:in call' actionpack (5.0.0.rc1) lib/action_dispatch/middleware/request_id.rb:24:incall'
rack (2.0.0.rc1) lib/rack/runtime.rb:22:in call' activesupport (5.0.0.rc1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:incall'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/executor.rb:12:in call' actionpack (5.0.0.rc1) lib/action_dispatch/middleware/static.rb:136:incall'
rack (2.0.0.rc1) lib/rack/sendfile.rb:111:in call' rack-cors (0.4.0) lib/rack/cors.rb:80:incall'
railties (5.0.0.rc1) lib/rails/engine.rb:522:in call' puma (3.4.0) lib/puma/configuration.rb:224:incall'
puma (3.4.0) lib/puma/server.rb:569:in handle_request' puma (3.4.0) lib/puma/server.rb:406:inprocess_client'
puma (3.4.0) lib/puma/server.rb:271:in block in run' puma (3.4.0) lib/puma/thread_pool.rb:114:inblock in spawn_thread'

根据要求,这是我的应用程序控制器。 omn​​iauth 控制器是 gem 提供的默认控制器。我没有修改它们。

class ApplicationController < ActionController::API
  include DeviseTokenAuth::Concerns::SetUserByToken

  before_action :configure_permitted_parameters, if: :devise_controller?

  before_action :authenticate_user_with_filter

  #this method will authenticate users using devise_token_auth unless
  #the controller is one of devise_token_auth
  #authentication will look for uid, client and token in header calls
  #and return 401 on no match
  def authenticate_user_with_filter
    if params[:controller]
      if !params[:controller].start_with? 'devise_token_auth'
        authenticate_user!
      end
    end
  end

  protected

    def configure_permitted_parameters
      devise_parameter_sanitizer.permit(:sign_up, keys: [:country_id, :fb_id, :fb_access_token, :code, :provider, :uid, :state, :omniauth_callback])
    devise_parameter_sanitizer.permit(:account_update, keys: [:country_id, :fb_id, :fb_access_token])

    end

end

顺便说一句,我确实设法通过直接从我的 Gemfile 中的 Github 指向最新的 devise_token_auth master 解决了这个特殊问题。最近肯定有什么问题被修复了,因为错误刚刚消失了,尽管版本完全相同,这看起来很奇怪。

但是,由于不同的原因,我现在收到不同的错误。我应该 post 将它作为一个不同的问题并自己回答这个问题吗?

Started GET "/auth/facebook/callback?code=AQCLHSxZ1xmAuvfIlWNU7N3edIHOPKEeBpKQDi1cbNa6ycj3lhBYKGQTjMfYIrwvDnQCdxDtLms91yG4A4fvnjiA0Afreq_d7JooMZEXcCb_gV-k2lpWSEsgRcWJmTdFNQtxnfTIxSJYBQriRtJvPiUVuJPootNEY3-14_1F3hpIzhjf9SEQXhLSLVKboLmnKd-IdnFGlx5yKBlKFbJywIzPRCokfegKlgZGPUQ1ipn3_qDCyTdAWs2Kd-cGopnohKntV8Qk2Dq3bMfDoI8p5oJl5snNf5WgnQ281xWlIS6E2KEFY-wJil4FqWxWuaawinc&state=9a9ef8294b902212af9e096be513755843660a6c20425afd" for 127.0.0.1 at 2016-05-30 18:01:47 -0500
Processing by DeviseTokenAuth::OmniauthCallbacksController#omniauth_success as */*
Parameters: {"code"=>"AQCLHSxZ1xmAuvfIlWNU7N3edIHOPKEeBpKQDi1cbNa6ycj3lhBYKGQTjMfYIrwvDnQCdxDtLms91yG4A4fvnjiA0Afreq_d7JooMZEXcCb_gV-k2lpWSEsgRcWJmTdFNQtxnfTIxSJYBQriRtJvPiUVuJPootNEY3-14_1F3hpIzhjf9SEQXhLSLVKboLmnKd-IdnFGlx5yKBlKFbJywIzPRCokfegKlgZGPUQ1ipn3_qDCyTdAWs2Kd-cGopnohKntV8Qk2Dq3bMfDoI8p5oJl5snNf5WgnQ281xWlIS6E2KEFY-wJil4FqWxWuaawinc", "state"=>"9a9ef8294b902212af9e096be513755843660a6c20425afd", "provider"=>"facebook", "omniauth_callback"=>{}}
Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms)

RuntimeError (No resource_class found):

/home/augusto/.rvm/gems/ruby-2.3.0@faces/bundler/gems/devise_token_auth-81fc7b40998d/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb:107:in `resource_class'
/home/augusto/.rvm/gems/ruby-2.3.0@faces/bundler/gems/devise_token_auth-81fc7b40998d/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb:235:in `get_resource_from_auth_hash'
/home/augusto/.rvm/gems/ruby-2.3.0@faces/bundler/gems/devise_token_auth-81fc7b40998d/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb:27:in `omniauth_success'
actionpack (5.0.0.rc1) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.0.0.rc1) lib/abstract_controller/base.rb:181:in `process_action'
actionpack (5.0.0.rc1) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.0.0.rc1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:126:in `call'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:455:in `call'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0.rc1) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.0.0.rc1) lib/action_controller/metal/rescue.rb:31:in `process_action'
actionpack (5.0.0.rc1) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.0.0.rc1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.0.rc1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.0.rc1) lib/active_support/notifications.rb:164:in `instrument'
actionpack (5.0.0.rc1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.0.0.rc1) lib/action_controller/metal/params_wrapper.rb:248:in `process_action'
activerecord (5.0.0.rc1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (5.0.0.rc1) lib/abstract_controller/base.rb:126:in `process'
actionpack (5.0.0.rc1) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (5.0.0.rc1) lib/action_controller/metal.rb:262:in `dispatch'
actionpack (5.0.0.rc1) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (5.0.0.rc1) lib/action_dispatch/routing/route_set.rb:32:in `serve'
actionpack (5.0.0.rc1) lib/action_dispatch/routing/mapper.rb:16:in `block in <class:Constraints>'
actionpack (5.0.0.rc1) lib/action_dispatch/routing/mapper.rb:46:in `serve'
actionpack (5.0.0.rc1) lib/action_dispatch/journey/router.rb:39:in `block in serve'
actionpack (5.0.0.rc1) lib/action_dispatch/journey/router.rb:26:in `each'
actionpack (5.0.0.rc1) lib/action_dispatch/journey/router.rb:26:in `serve'
actionpack (5.0.0.rc1) lib/action_dispatch/routing/route_set.rb:725:in `call'
actionview (5.0.0.rc1) lib/action_view/digestor.rb:12:in `call'
omniauth (1.3.1) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.3.1) lib/omniauth/strategy.rb:164:in `call'
omniauth (1.3.1) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.3.1) lib/omniauth/strategy.rb:164:in `call'
omniauth (1.3.1) lib/omniauth/builder.rb:63:in `call'
rack-attack (4.4.1) lib/rack/attack.rb:100:in `call'
rack (2.0.0.rc1) lib/rack/session/abstract/id.rb:222:in `context'
rack (2.0.0.rc1) lib/rack/session/abstract/id.rb:216:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/cookies.rb:613:in `call'
warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
warden (1.2.6) lib/warden/manager.rb:34:in `catch'
warden (1.2.6) lib/warden/manager.rb:34:in `call'
rack (2.0.0.rc1) lib/rack/etag.rb:25:in `call'
rack (2.0.0.rc1) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.0.rc1) lib/rack/head.rb:12:in `call'
activerecord (5.0.0.rc1) lib/active_record/migration.rb:552:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.0.rc1) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.0.rc1) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.0.rc1) lib/active_support/tagged_logging.rb:70:in `block in tagged'
activesupport (5.0.0.rc1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.0.rc1) lib/active_support/tagged_logging.rb:70:in `tagged'
railties (5.0.0.rc1) lib/rails/rack/logger.rb:24:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/request_id.rb:24:in `call'
rack (2.0.0.rc1) lib/rack/runtime.rb:22:in `call'
activesupport (5.0.0.rc1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/static.rb:136:in `call'
rack (2.0.0.rc1) lib/rack/sendfile.rb:111:in `call'
rack-cors (0.4.0) lib/rack/cors.rb:80:in `call'
railties (5.0.0.rc1) lib/rails/engine.rb:522:in `call'
puma (3.4.0) lib/puma/configuration.rb:224:in `call'
puma (3.4.0) lib/puma/server.rb:569:in `handle_request'
puma (3.4.0) lib/puma/server.rb:406:in `process_client'
puma (3.4.0) lib/puma/server.rb:271:in `block in run'
puma (3.4.0) lib/puma/thread_pool.rb:114:in `block in spawn_thread'

这个问题

ArgumentError (Before process_action callback :set_user_by_token has not been defined)

通过将最新的 devise_token_auth master 直接指向 GemfileGithub 中的

来修复
gem 'devise_token_auth', :git => 'git://github.com/lynndylanhurley/devise_token_auth.git'

运行 包更新,错误消失。

我的后续错误是我的应用程序特有的路由问题