[英]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
应用程序/控制器/ 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
希望这对你有用。
我认为问题总是出在按钮或锚点上,所以使用这个
<%= link_to user_facebook_omniauth_authorize_path, method: :post do %>
login with facebook
<% end %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.