我有omniauth和devise_token_auth的问题。当我尝试用facebook注册用户时,回调无法正常工作。
她的 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
这是 app / controllers / users / 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):
我错过了一些简单的东西吗?我一直在寻找最近几天的解决方案
答案 0 :(得分:1)
问题出在您的OmniauthCallbacksController
。
一种方法是从DeviseTokenAuth::OmniauthCallbacksController
继承控制器并挂钩操作:
class Users::OmniauthCallbacksController < DeviseTokenAuth::OmniauthCallbacksController
def redirect_callbacks
super
# some logic here
end
end
其他方法是从code gem复制整个控制器devise_token_auth
并解决它们:
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