使用 Devise 的 Omniauth-facebook:"Missing passthru" 个错误

Omniauth-facebook with Devise: "Missing passthru" errors

我安装了 Devise 身份验证,没有任何问题。现在我正在尝试添加一个选项来使用 Facebook 登录,使用 Omniauth-facebook。

我按照 this guide 中的说明进行操作,但在访问 url localhost:3000/auth/facebook.

时收到有关缺少 "Passthru" 文档的错误

这是我遇到的第一个错误:

Unknown action
The action 'passthru' could not be found for RegistrationsController

我尝试通过向我的控制器添加一个空 "passthru" 操作来修复创可贴:

def passthru
end

这解决了那个错误,但我在 return:

中得到了一个不同的错误
Template is missing
Missing template registrations/passthru, devise/registrations/passthru, devise/passthru, application/passthru with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in: * "/home/user/project/app/views" * "/home/user/.rvm/gems/ruby-2.0.0-p648@railstutorial_rails_4_0/gems/devise-3.5.2/app/views"

我尝试在指定的文件夹中创建 "passthru.html.erb",但错误仍然存​​在。无论如何,我认为这些错误是更深层次问题的象征。

还有其他人 运行 遇到过这个问题吗?我在上面能找到的只有 this SO question,但 none 的答案很有帮助。


到目前为止我的代码:

Gemfile

gem 'devise'
gem 'omniauth-facebook'
gem 'omniauth'

routes.rb

devise_for :members, controllers: { 注册: 'registrations', omniauth_callbacks: 'registrations' }

member.rb

  devise :database_authenticatable, :registerable,
         :omniauthable, :omniauth_providers => [:facebook]


  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |member|
      member.email = auth.info.email
      member.password = Devise.friendly_token[0,20]
      member.title = auth.info.name
    end
  end

registrations_controller.rb

  def facebook
    @member = Member.from_omniauth(request.env["omniauth.auth"])
    if @member.persisted?
      sign_in_and_redirect @member, :event => :authentication
      set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_member_registration_url
    end
  end

  def failure
    redirect_to root_path
  end

  def passthru
  end

initializers/devise.rb

config.omniauth :facebook, "<app_id>", "<app_secret>"

您可以为 Omniauth callback

创建另一个控制器
class OmniauthCallbacksController < Devise::OmniauthCallbacksController

  def facebook
    @user =  User.from_omniauth(request.env['omniauth.auth'])
    if @user.persisted?
      sign_in_and_redirect @user, :event => :authentication 
      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 after_sign_in_path_for(resource)
    super resource
  end

end

您需要将路线重置为

devise_for :members, :controllers => {:registrations => "members/registrations", :omniauth_callbacks => 'omniauth_callbacks'}

我记得你不需要在 member.rb

中使用 :omniauth_providers => [:facebook]

现在您可以在 sign_up page 中添加一个按钮,或者在您的 devise/shared/_links.html.erb 中包含以下代码,因为它也可以在您的 sign_in 表单中使用。

<%- if devise_mapping.omniauthable? %>
  <%- resource_class.omniauth_providers.each do |provider| %>
    <%= link_to "Sign up with #{provider.to_s.titleize}", omniauth_authorize_path(resource_name, provider), class: "btn btn-default navbar-btn" %><br />
  <% end -%>
<% end -%>

你还需要在初始化器中配置 devise

在你的/config/initializers/devise.rb

config.omniauth :facebook, "App ID", "App Secret", scope: 'email', info_fields: 'email,name'

请使用 facebook Link

完成 Sing_up 这个最简单的教程

试试这个......

config/initializers/devise.rb

config.omniauth :facebook, ENV["FACEBOOK_KEY"], ENV["FACEBOOK_SECRET"], { :scope => 'email, offline_access'}

config/routes.rb

devise_for :members, controllers: { registrations: 'registrations', omniauth_callbacks: "omniauth_callbacks" }

app/models/member.rb

devise :database_authenticatable, :registerable,
      :recoverable, :rememberable, :trackable, :validatable, :omniauthable


def self.from_omniauth(auth)
  where(provider: auth.provider, uid: auth.uid).first_or_create do |member|
    member.email = auth.info.email
    member.password = Devise.friendly_token[0,20]
    member.title = auth.info.name
  end
end 

app/controllers/omniauth_callbacks_controller.rb

  skip_before_filter :authenticate_user!

  def facebook
    p env["omniauth.auth"]
    user = User.from_omniauth(env["omniauth.auth"])
    if user.persisted?
      flash[:notice] = "You are in..!!!"
      sign_in_and_redirect(user)
    else
      session["devise.user_attributes"] = user.attributes
      redirect_to new_user_registration_url
    end
  end

  def failure
    #handle you logic here..
    #and delegate to super.
    super
  end

希望这对你有用。

Passthru 是 omniauth 的遗物更新你的 devise omniauth 等宝石。 有一个控制器叫 omiauth_callback 这是发出噪音的那个;P.(可以帮助你追查问题的根源)

如果您像这样在控制器中创建一个方法: def passthru end 您必须使用(甚至是空的)或重定向创建视图:从 ajax 技术中获得灵感以绕过 html 渲染。 希望它能带你走上解决问题的道路。

也试试这些路线: ``` user_omniauth_authorize /users/auth/:provider(.:format) sessions#passthru {:provider=>/facebook|twitter|google/}

user_omniauth_callback /users/auth/:action/callback(.:format) 会话#(?-mix:facebook|twitter|google) ```

试试这个:

更新 GemFile:

gem 'omniauth-facebook'
gem 'omniauth'

转到 rails_apps/yourapp/config/initializers/devise.rb

Devise.setup do |config|
   config.omniauth :facebook, "KEY", "SECRET"
 end

更新用户模型

class User < ActiveRecord::Base

         devise :database_authenticatable, :registerable,
        :recoverable, :rememberable, :trackable, :validatable,
    :omniauthable, :omniauth_providers => [:facebook]

     def self.from_omniauth(auth)
         where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
           user.provider = auth.provider
           user.uid = auth.uid
           user.email = auth.info.email
           user.password = Devise.friendly_token[0,20]
         end
     end
    end

转到:rails_apps/yourapp/config/routes.rb

Rails.application.routes.draw do
  devise_for :users
  resources :users
end

在视图中编辑

 <%= link_to "Sign in with Facebook", "/auth/facebook", id: "sign_in" %>

我认为问题总是出在按钮或锚点上,所以使用这个

<%= link_to user_facebook_omniauth_authorize_path, method: :post do %>
login with facebook 
<% end %>