巫术 gem Facebook OAuth 返回 Nil 电子邮件
Sorcery gem Facebook OAuth returning Nil Email
我正在使用 Sorcery 0.9.1 和 (Rails 4.2.1 & Ruby 2.2.2) 上的外部子模块来执行 Facebook OAuth,但我遇到了问题。
我几乎一字不差地按照说明操作,但它失败了。所以我取出了 oauths_controller
上的 rescue
并且我能够对其进行一些调试。现在它给我一个数据库错误,电子邮件不能为空(这是正确的,我不想要用户 w/o 电子邮件)。 为什么 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
在此先感谢您的帮助,如果您还需要什么,请告诉我。
我找到了修复:https://github.com/NoamB/sorcery/issues/703
显然这是 Facebook API v 2.4 的问题。现在您必须更明确地说明用户路径,所以我刚刚添加了
config.facebook.user_info_path = "me?fields=email,first_name,last_name"
现在可以使用了。我为不检查巫术问题而感到愚蠢。菜鸟错误。
我正在使用 Sorcery 0.9.1 和 (Rails 4.2.1 & Ruby 2.2.2) 上的外部子模块来执行 Facebook OAuth,但我遇到了问题。
我几乎一字不差地按照说明操作,但它失败了。所以我取出了 oauths_controller
上的 rescue
并且我能够对其进行一些调试。现在它给我一个数据库错误,电子邮件不能为空(这是正确的,我不想要用户 w/o 电子邮件)。 为什么 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
在此先感谢您的帮助,如果您还需要什么,请告诉我。
我找到了修复:https://github.com/NoamB/sorcery/issues/703
显然这是 Facebook API v 2.4 的问题。现在您必须更明确地说明用户路径,所以我刚刚添加了
config.facebook.user_info_path = "me?fields=email,first_name,last_name"
现在可以使用了。我为不检查巫术问题而感到愚蠢。菜鸟错误。