omniauth.auth Facebook 未返回完整哈希值且无法使用 Facebook 成功登录
omniauth.auth for Facebook not returning full hash and not able to successfully login with facebook
我正在研究来自 http://www.theodinproject.com/ruby-on-rails/final-project 的 facebook 克隆项目。
我被 omniauth-facebook 部分卡住了,无法成功登录 facebook。我认为问题可能是由于 request.env["omniauth.auth"]。当我尝试提高 request.env["omniauth.auth"].to_yaml 时。我得到以下不完整的哈希值。它缺少很多信息,例如first_name、last_name、性别等
--- !ruby/hash:OmniAuth::AuthHash
provider: facebook
uid: '10206926404981253'
info: !ruby/hash:OmniAuth::AuthHash::InfoHash
name: Thomas Pan
image: http://graph.facebook.com/10206926404981253/picture
credentials: !ruby/hash:OmniAuth::AuthHash
token: <token>
expires_at: 1442277104
expires: true
extra: !ruby/hash:OmniAuth::AuthHash
raw_info: !ruby/hash:OmniAuth::AuthHash
name: Thomas Pan
id: <id>
** 为了安全起见,用 <> 替换了一些信息。
我也在使用它和设计。
其他一切似乎都已正确设置,因为我已按照此处的说明进行设计和 omniauth-facebook。 https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
user.rb
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.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.first_name = auth.info.first_name
user.last_name = auth.info.last_name
user.gender = auth.extra.raw.gender
end
end
devise.rb
config.omniauth :facebook, ENV['FB_APP_ID'], ENV['FB_APP_SECRET']
routes.rb
devise_for :users, :controllers => { :registrations => "users/registrations", :omniauth_callbacks => "users/omniauth-callbacks" }
omniauth_callbacks_controller.rb
def facebook
raise request.env['omniauth.auth'].to_yaml
end
end
如有任何帮助,我们将不胜感激!
版本信息:
Rails 4.2.1
Ruby2.0.0
创建一个 OmniauthCallbacksController 并添加以下代码
class OmniauthCallbacksController < ApplicationController
skip_before_filter :authenticate_user!
def all
p env["omniauth.auth"]
user = User.from_omniauth(env["omniauth.auth"])
if user.persisted?
# flash[:alert] = "You have to confirm your account before continuing."
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.
redirect_to new_user_registration_url
end
alias_method :facebook, :all
end
在你的 config/routes.rb
devise_for :users, controllers: { omniauth_callbacks: "omniauth_callbacks" }
创建授权模型
rails g model Authorization
在迁移中添加以下代码
class CreateAuthorizations < ActiveRecord::Migration
def change
create_table :authorizations do |t|
t.string :provider
t.string :uid
t.integer :user_id
t.string :token
t.string :secret
t.timestamps
end
end
end
然后
rake db:migrate
在你的models/authorization.rb
belongs_to :user
在你的models/user.rb
has_many :authorizations
def self.from_omniauth(auth)
authorization = Authorization.where(:provider => auth.provider, :uid => auth.uid.to_s).first_or_initialize
authorization.token = auth.credentials.token
if authorization.user.blank?
user = User.where('email = ?', auth["info"]["email"]).first
if user.blank?
user = User.new
user.password = Devise.friendly_token[0,10]
user.email = auth.info.email
user.save
end
authorization.user_id = user.id
end
authorization.save
authorization.user
end
希望对您有所帮助。
在devise.rb
中:
config.omniauth :facebook, ENV['FB_APP_ID'], ENV['FB_APP_SECRET'], scope: 'email', info_fields: 'email,name,first_name,last_name,gender'
我正在研究来自 http://www.theodinproject.com/ruby-on-rails/final-project 的 facebook 克隆项目。
我被 omniauth-facebook 部分卡住了,无法成功登录 facebook。我认为问题可能是由于 request.env["omniauth.auth"]。当我尝试提高 request.env["omniauth.auth"].to_yaml 时。我得到以下不完整的哈希值。它缺少很多信息,例如first_name、last_name、性别等
--- !ruby/hash:OmniAuth::AuthHash
provider: facebook
uid: '10206926404981253'
info: !ruby/hash:OmniAuth::AuthHash::InfoHash
name: Thomas Pan
image: http://graph.facebook.com/10206926404981253/picture
credentials: !ruby/hash:OmniAuth::AuthHash
token: <token>
expires_at: 1442277104
expires: true
extra: !ruby/hash:OmniAuth::AuthHash
raw_info: !ruby/hash:OmniAuth::AuthHash
name: Thomas Pan
id: <id>
** 为了安全起见,用 <> 替换了一些信息。
我也在使用它和设计。
其他一切似乎都已正确设置,因为我已按照此处的说明进行设计和 omniauth-facebook。 https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
user.rb
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.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.first_name = auth.info.first_name
user.last_name = auth.info.last_name
user.gender = auth.extra.raw.gender
end
end
devise.rb
config.omniauth :facebook, ENV['FB_APP_ID'], ENV['FB_APP_SECRET']
routes.rb
devise_for :users, :controllers => { :registrations => "users/registrations", :omniauth_callbacks => "users/omniauth-callbacks" }
omniauth_callbacks_controller.rb
def facebook
raise request.env['omniauth.auth'].to_yaml
end
end
如有任何帮助,我们将不胜感激!
版本信息: Rails 4.2.1 Ruby2.0.0
创建一个 OmniauthCallbacksController 并添加以下代码
class OmniauthCallbacksController < ApplicationController
skip_before_filter :authenticate_user!
def all
p env["omniauth.auth"]
user = User.from_omniauth(env["omniauth.auth"])
if user.persisted?
# flash[:alert] = "You have to confirm your account before continuing."
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.
redirect_to new_user_registration_url
end
alias_method :facebook, :all
end
在你的 config/routes.rb
devise_for :users, controllers: { omniauth_callbacks: "omniauth_callbacks" }
创建授权模型
rails g model Authorization
在迁移中添加以下代码
class CreateAuthorizations < ActiveRecord::Migration
def change
create_table :authorizations do |t|
t.string :provider
t.string :uid
t.integer :user_id
t.string :token
t.string :secret
t.timestamps
end
end
end
然后
rake db:migrate
在你的models/authorization.rb
belongs_to :user
在你的models/user.rb
has_many :authorizations
def self.from_omniauth(auth)
authorization = Authorization.where(:provider => auth.provider, :uid => auth.uid.to_s).first_or_initialize
authorization.token = auth.credentials.token
if authorization.user.blank?
user = User.where('email = ?', auth["info"]["email"]).first
if user.blank?
user = User.new
user.password = Devise.friendly_token[0,10]
user.email = auth.info.email
user.save
end
authorization.user_id = user.id
end
authorization.save
authorization.user
end
希望对您有所帮助。
在devise.rb
中:
config.omniauth :facebook, ENV['FB_APP_ID'], ENV['FB_APP_SECRET'], scope: 'email', info_fields: 'email,name,first_name,last_name,gender'