2016-05-14 2 views
0

私はomniauthとdevise_token_authに問題があります。 Facebookにユーザーを登録しようとすると、コールバックが機能しません。Users :: OmniauthCallbacksControllerの 'redirect_callbacks'アクションが見つかりません

彼女は私のGemfileです:

gem 'rack-cors', :require => 'rack/cors' 
gem 'devise_token_auth' 
gem 'omniauth' 
gem 'omniauth-facebook' 
gem 'devise', '~> 3.2' 

routes.rbを

Rails.application.routes.draw do 
    namespace :api, defaults: { format: :json } do 
     scope :v1 do 
     mount_devise_token_auth_for 'User', at: 'auth', :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } 
     match 'auth/:provider/callback', to: 'sessions#create', via: [:get, :post] 
     end 
    end 
    root to:'welcomes#index' 
end 

ここアプリ/コントローラ/ユーザ/ omniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController 
    def facebook 
    # You need to implement the method below in your model (e.g. app/models/user.rb) 
    @user = User.from_omniauth(request.env["omniauth.auth"]) 

    if @user.persisted? 
     sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated 
     set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format? 
    else 
     session["devise.facebook_data"] = request.env["omniauth.auth"] 
     redirect_to new_user_registration_url 
    end 
    end 

    def failure 
    redirect_to root_path 
    end 
end 
です

設定/初期化子/ omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do 
    provider :facebook, "XXX", "XXX", 
    :scope => 'public_profile,email,user_birthday', 
    :info_fields => 'id,about,birthday,email,first_name,gender,last_name' 
end 

そして最後に、これは、コンソールの応答です:

Started GET "/api/v1/auth/facebook" for 192.168.33.1 at 2016-05-14 10:26:00 +0000 
Cannot render console from 192.168.33.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 


Started GET "/omniauth/facebook?resource_class=User" for 192.168.33.1 at 2016-05-14 10:26:00 +0000 
Cannot render console from 192.168.33.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 
I, [2016-05-14T10:26:00.995076 #3226] INFO -- omniauth: (facebook) Request phase initiated. 


Started GET "/omniauth/facebook/callback?code=XXX" for 192.168.33.1 at 2016-05-14 10:26:01 +0000 
Cannot render console from 192.168.33.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 
I, [2016-05-14T10:26:01.130357 #3226] INFO -- omniauth: (facebook) Callback phase initiated. 

AbstractController::ActionNotFound (The action 'redirect_callbacks' could not be found for Users::OmniauthCallbacksController): 

私は、単純な何かが足りないのですか?私は最後の数日間の解決策を探しています

答えて

1

問題はあなたのOmniauthCallbacksControllerにあります。

一つの方法は、DeviseTokenAuth::OmniauthCallbacksControllerから、コントローラを継承することで、アクションにフック:

class Users::OmniauthCallbacksController < DeviseTokenAuth::OmniauthCallbacksController 
    def redirect_callbacks 
     super 
     # some logic here 
    end 
end 

他の方法は、彼らの周りdevise_token_auth宝石や仕事から全体コントローラcodeをコピーすることです:

module DeviseTokenAuth 
    class OmniauthCallbacksController < DeviseTokenAuth::ApplicationController 

    attr_reader :auth_params 
    skip_before_action :set_user_by_token, raise: false 
    skip_after_action :update_auth_header 

    # intermediary route for successful omniauth authentication. omniauth does 
    # not support multiple models, so we must resort to this terrible hack. 
    def redirect_callbacks 

     # derive target redirect route from 'resource_class' param, which was set 
     # before authentication. 
     devise_mapping = [request.env['omniauth.params']['namespace_name'], 
         request.env['omniauth.params']['resource_class'].underscore.gsub('/', '_')].compact.join('_') 
     redirect_route = "#{request.protocol}#{request.host_with_port}/#{Devise.mappings[devise_mapping.to_sym].fullpath}/#{params[:provider]}/callback" 

     # preserve omniauth info for success route. ignore 'extra' in twitter 
     # auth response to avoid CookieOverflow. 
     session['dta.omniauth.auth'] = request.env['omniauth.auth'].except('extra') 
     session['dta.omniauth.params'] = request.env['omniauth.params'] 

     redirect_to redirect_route 
    end 

    # some code here 

    end 
end 
+0

ありがとう!それはうまくいっている! – Shinix

関連する問題