gpt4 book ai didi

ruby-on-rails - 巫术 gem Facebook OAuth 返回无电子邮件

转载 作者:数据小太阳 更新时间:2023-10-29 08:36:59 25 4
gpt4 key购买 nike

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

我几乎一字不差地按照说明进行操作,但它失败了。所以我取出了 oauths_controller 上的 rescue 并且我能够对其进行一些调试。现在它给我一个数据库错误,电子邮件不能为空(这是正确的,我不想要没有电子邮件的用户)。 为什么 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"

现在可以正常使用了。我为不检查巫术问题而感到愚蠢。菜鸟错误。

关于ruby-on-rails - 巫术 gem Facebook OAuth 返回无电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31909990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com