简体   繁体   English

带有 Devise 的 Omniauth-facebook:“缺少直通”错误

[英]Omniauth-facebook with Devise: "Missing passthru" errors

I have Devise authentication installed with no problems.我安装了 Devise 身份验证没有问题。 Now I'm trying to add an option to log in with Facebook, using Omniauth-facebook.现在我正在尝试使用 Omniauth-facebook 添加一个选项以使用 Facebook 登录。

I followed the instructions inthis guide , but I'm getting errors about missing "Passthru" documentation, when visiting the url localhost:3000/auth/facebook .我按照本指南中的说明进行操作,但在访问 url localhost:3000/auth/facebook时遇到有关缺少“Passthru”文档的错误。

Here's the first error I got:这是我得到的第一个错误:

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

I tried a bandaid fix by just adding an empty "passthru" action to my controller:我尝试通过向我的 controller 添加一个空的“passthru”操作来修复创可贴:

def passthru
end

And that resolved that error, but I got a different one in 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"

I tried creating a "passthru.html.erb" in the stated folders, but that error remained.我尝试在指定的文件夹中创建一个“passthru.html.erb”,但该错误仍然存在。 In any case, I think these errors are emblematic of a deeper problem.无论如何,我认为这些错误象征着更深层次的问题。

Has anyone else run into this problem?还有其他人遇到过这个问题吗? All I could find on it was this SO question , but none of the answers were helpful.我所能找到的就是这个 SO question ,但没有一个答案有帮助。


My code so far:到目前为止我的代码:

Gemfile宝石文件

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

routes.rb路线.rb

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

member.rb成员.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注册控制器.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初始值设定项/devise.rb

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

Try this : 试试这个 :

Update GemFile: 更新GemFile:

gem 'omniauth-facebook'
gem 'omniauth'

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

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

Update the User Model 更新用户模型

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

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

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

Edit in View 在视图中编辑

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

Passthru is a relic from omniauth update your gems of devise omniauth and so on . Passthru是omniauth的遗物,更新你的设计omniauth宝石等等。 there is a controller called omiauth_callback this is the one makes noizes ;P.(may help you trace the problem's source) 有一个名为omiauth_callback的控制器,这是一个发出noize; P。(可以帮助你追踪问题的来源)

If you create a method in controller like so : def passthru end You HAVE TO create a view with(even empty), or redirection :get inspired by ajax techniques to bypass html rendering . 如果你在控制器中创建一个类似的方法: def passthru end你必须创建一个带(甚至是空的)或重定向的视图:从ajax技术中获取灵感来绕过html渲染。 Hope it send you on the way of problem solving . 希望它能帮助你解决问题。

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

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

You can create another controller for Omniauth callback 您可以为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

you need to reset your routes as 你需要重置你的路线

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

As i could remember you need not to use :omniauth_providers => [:facebook] in your member.rb 我记得你不需要在你的member.rb使用:omniauth_providers => [:facebook]

Now you can add a button in your sign_up page or instead include the below code in your devise/shared/_links.html.erb , because it will be available in your sign_in form also. 现在,您可以在sign_up page添加一个按钮,或者在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 -%>

you also need to configure devise in initializers 您还需要在初始化程序中配置设计

In your /config/initializers/devise.rb /config/initializers/devise.rb

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

Please go through this simplest tutorial for Sing_up with facebook Link 请通过facebook Link查看Sing_up最简单的教程

Try this ......... 尝试这个 .........

config/initializers/devise.rb 配置/初始化/ devise.rb

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

config/routes.rb 配置/ routes.rb中

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

app/models/member.rb 应用程序/模型/ 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 应用程序/控制器/ omn​​iauth_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

Hope this will work for you. 希望这对你有用。

I think the problem is always on the button or the anchor, so use this我认为问题总是出在按钮或锚点上,所以使用这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM