巫术宝石Facebook OAuth返回Nil Email

时间:2015-08-09 23:59:30

标签: ruby-on-rails ruby facebook oauth sorcery

我正在使用Sorcery 0.9.1和外部子模块(Rails 4.2.1& Ruby 2.2.2)进行Facebook OAuth,我遇到了问题。

我几乎逐字地按照说明操作,但它失败了。所以我取出了rescue上的oauths_controller,我能够稍微调试一下。现在它给了我一个数据库错误,电子邮件不能为空(这是正确的,我不希望用户没有电子邮件)。 为什么Sorcery会向我null发送电子邮件?

这是确切的错误:

ERROR:  null value in column "email" violates not-null constraint
DETAIL:  Failing row contains (7, null, null, null, 2015-08-09 23:58:01.39694, 2015-08-09 23:58:01.39694, null, null, null, null, null, f).

以下是相关文件:

# oauths_controller.rb
class OauthsController < ApplicationController
  skip_before_filter :require_login

  def oauth
    session[:return_to_url] = request.referer unless request.referer =~ /oauth/
    login_at(auth_params[:provider])
  end

  def callback
    provider = auth_params[:provider]
    if @user = login_from(provider)
      redirect_back_or_to root_path, :notice => "Logged in from #{provider.titleize}!"
    else
      begin
        @user = create_from(provider)
        reset_session
        auto_login(@user)
        redirect_back_or_to root_path, :notice => "Logged in from #{provider.titleize}!"
      # rescue
        # redirect_back_or_to root_path, :alert => "Failed to login from #{provider.titleize}!"
      end
    end
  end

private
  def auth_params
    params.permit(:provider, :code)
  end
end

# user.rb
class User < ActiveRecord::Base
  authenticates_with_sorcery! do |config|
    config.authentications_class = Authentication
  end

  has_many :authentications, :dependent => :destroy
  accepts_nested_attributes_for :authentications

  has_many :families

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i

  validates :email, presence: true, format: VALID_EMAIL_REGEX,uniqueness: { case_sensitive: false }
  validates :password, presence: true, length: { minimum: 6 }

  def has_family?
    self.families.any?
  end
private
  def user_params
    params.permit(:user).require(:email, :password, :authentications_attributes [:user_id, :provider, :uid])
  end
end

# sorcery.rb
Rails.application.config.sorcery.submodules = [:external]

Rails.application.config.sorcery.configure do |config|

  config.external_providers = [:facebook]

  config.facebook.key = ENV['FACEBOOK_KEY']
  config.facebook.secret = ENV['FACEBOOK_SECRET']
  config.facebook.callback_url = "http://localhost:5000/oauth/callback?provider=facebook"
  config.facebook.user_info_mapping = {:email => 'email'}
  config.facebook.scope = "email,public_profile"
  config.facebook.display = "page"
  config.facebook.api_version = "v2.4"

  config.user_config do |user|
    user.authentications_class = Authentication
  end
  config.user_class = User
end

以下是整个回购,以防有用:https://github.com/carterdea/adopttogether

提前感谢您的帮助,如果您还有其他需要,请与我们联系。

1 个答案:

答案 0 :(得分:4)

我找到了修复:https://github.com/NoamB/sorcery/issues/703

显然它是Facebook API v 2.4问题。现在你必须更明确地了解用户路径,所以我刚刚添加了

config.facebook.user_info_path = "me?fields=email,first_name,last_name"

它现在正在工作。我没有检查巫术问题,我感到愚蠢。 Noob错误。