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

时间:2016-05-30 20:37:41

标签: ruby-on-rails devise rails-api ruby-on-rails-5 omniauth-facebook

我正在使用setup devise_token_auth作为移动应用的API后端。

我正在使用:

  • 设计(4.0.3)
  • devise_token_auth(0.1.37)
  • omniauth(1.3.1)
  • omniauth-facebook(3.0.0)
  • Rails 5.0.0.rc1,仅提供API选项

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

为了在客户端正确实现Facebook登录,并且仍能在服务器上进行身份验证,我遵循omniauth-gem中指示的方法:

https://github.com/mkdynamic/omniauth-facebook 请参阅章节:使用Facebook Javascript SDK的客户端流程

基本上说是处理客户端中的所有登录,一旦用户登录,将客户端重定向到auth / facebook / callback的API回调URL(或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)时遇到此错误,无论是否存在任何参数/ cookie:

**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来解决这个问题。最近必须修复一些东西,因为错误消失了,虽然版本完全相同但看起来很奇怪。

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

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'

1 个答案:

答案 0 :(得分:1)

这个特殊问题

ArgumentError (Before process_action callback :set_user_by_token has not been defined)
通过在Gemfile中直接指向最新的devise_token_auth master到Github来修复

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

运行捆绑包更新,错误消失。

我后续的错误是一个路由问题,这对我的应用来说是特别的