簡體   English   中英

帶有 Devise 的 Omniauth-facebook:“缺少直通”錯誤

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

我安裝了 Devise 身份驗證沒有問題。 現在我正在嘗試使用 Omniauth-facebook 添加一個選項以使用 Facebook 登錄。

我按照本指南中的說明進行操作,但在訪問 url localhost:3000/auth/facebook時遇到有關缺少“Passthru”文檔的錯誤。

這是我得到的第一個錯誤:

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

我嘗試通過向我的 controller 添加一個空的“passthru”操作來修復創可貼:

def passthru
end

這解決了那個錯誤,但我得到了一個不同的回報:

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”,但該錯誤仍然存在。 無論如何,我認為這些錯誤象征着更深層次的問題。

還有其他人遇到過這個問題嗎? 我所能找到的就是這個 SO question ,但沒有一個答案有幫助。


到目前為止我的代碼:

寶石文件

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

路線.rb

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

成員.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

注冊控制器.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

初始值設定項/devise.rb

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

試試這個 :

更新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" %>

Passthru是omniauth的遺物,更新你的設計omniauth寶石等等。 有一個名為omiauth_callback的控制器,這是一個發出noize; 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)sessions#(? - mix:facebook | twitter | google)```

您可以為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添加一個按鈕,或者在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 -%>

您還需要在初始化程序中配置設計

/config/initializers/devise.rb

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

請通過facebook Link查看Sing_up最簡單的教程

嘗試這個 .........

配置/初始化/ devise.rb

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

配置/ routes.rb中

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

應用程序/模型/ 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 

應用程序/控制器/ 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

希望這對你有用。

我認為問題總是出在按鈕或錨點上,所以使用這個

<%= 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