ruby-on-railsrubyfacebookoauthsorcery

Sorcery gem Facebook OAuth returning Nil Email


I'm using Sorcery 0.9.1 and the External submodule on (Rails 4.2.1 & Ruby 2.2.2) to do Facebook OAuth and I'm having issues.

I followed the instructions almost verbatim, and it was failing. So I took out the rescue on the oauths_controller and i was able to debug it a bit. Now it's giving me a database error that email can't be null (which is correct, I don't want a user w/o an email). Why is Sorcery giving me null for email?

Here is the exact error:

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).

Here are the relevant files:

# 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

Here's the entire repo in case that's helpful: https://github.com/carterdea/adopttogether

Thanks in advance for your help and let me know if you need anything else.


Solution

  • I found the fix: https://github.com/NoamB/sorcery/issues/703

    Apparently it's a Facebook API v 2.4 issue. Now you have to be more explicit about the user path, so I just added

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

    and it's working now. I feel dumb for not checking the sorcery issues. Noob mistake.