【问题标题】:Heroku + Devise 1.2 + OmniAuth to GitHub = null value in column "email"Heroku + Devise 1.2 + OmniAuth 到 GitHub = “电子邮件”列中的空值
【发布时间】:2011-06-22 11:58:28
【问题描述】:

我正在尝试设置 Devise 1.2 以允许通过 GitGub 进行用户身份验证。至于我可以使用 Cucumber 在本地对其进行测试,并剔除 GitHub OAuth,它似​​乎工作正常。但是,在我部署到 Heroku 并尝试进行身份验证后,我从 GitHub 重定向回我的应用程序后收到错误消息。

检查 Heroku 日志,我看到的错误是...

PGError: ERROR: "email" 列中的空值违反非空约束

显然,我要么没有从 GitHub 收到电子邮件地址,要么它在途中丢失了?

我已经按照 https://github.com/plataformatec/devise/wiki/OmniAuth:-Overviewhttps://github.com/plataformatec/devise/wiki/OmniAuth:--Testing-%27facebook%27-signup--%5BRails-3---Cucumber---Capybara---Mongoid-%5D 上的说明和示例进行了修改,但针对 GitHub 而不是 Facebook 进行了修改,请参阅 https://github.com/intridea/omniauth/blob/master/oa-oauth/lib/omniauth/strategies/github.rb 的 GitHub 策略的 OmniAuth 源代码。

这是我的 devise_steps.rb 文件的内容,它产生了一个通过 Cucumber 功能:

ACCESS_TOKEN = {
  :access_token => "stevejdev"
}

# Not all pieces of this hash are yet proven to be correct.
# It does, at least supply the necessary information for a
# successful login simulation though.
GITHUB_INFO = {
  :user => {
    :id => '12345', 
    :email => 'johndoe@example.com',
    # login value maps to nickname and to <user> part of
    # "http://github.com/<user>" in urls[GitHub]
    :login => 'johnxd',
    :name => 'John Doe',
    # blog value maps to urls[blog]
    :blog => 'http://blaagstop.com/johndoe',
  }
}

When /^GitHub replies$/ do 
  Devise::OmniAuth.short_circuit_authorizers!
  Devise::OmniAuth.stub!(:github) do |b|
    b.post('/login/oauth/access_token') { 
              [200, {}, ACCESS_TOKEN.to_json] }
    b.get('/api/v2/json/user/show?access_token=stevejdev') {
              [200, {}, GITHUB_INFO.to_json ] }
  end

  visit user_omniauth_callback_path(:github)
end

这是我的回调处理程序:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

  def github
    @user = User.find_for_github_oauth(env["omniauth.auth"], current_user)

    if @user.persisted?
      flash[:notice] = I18n.t(
        "devise.omniauth_callbacks.success", :kind => "GitHub" )
      sign_in_and_redirect @user, :event => :authentication
    else
      session["devise.github_data"] = env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end

end

这是我的用户模型中的 find_for_github_oauth 定义:

def self.find_for_github_oauth(access_token, signed_in_resource=nil)
  data = access_token['extra']['user_hash']

  # Find the existing user or create a new one.
  #  Omit the password-generation code shown in the example at
  #  https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
  #  because we're intending to use OmniAuth only, so
  #  presumably don't need a password.
  User.find_by_email( data["email"] ) ||
  User.create!( :email => data["email"] )

end

【问题讨论】:

    标签: ruby-on-rails github cucumber devise omniauth


    【解决方案1】:

    事实证明,问题在于 GitHub 并没有使用电子邮件地址作为用户标识符,也不一定在令牌中包含电子邮件值。我通过使用登录名而不是电子邮件地址解决了这个问题。

    由于我计划将来支持多个提供程序,因此我现在使用auth_providerlogin 字段的组合作为users 表的逻辑键,并且我指定了@987654324 @ 在 config/devise.rb 文件中。当我添加对另一个使用电子邮件地址进行用户登录的提供商的支持时,这很好。我将使用提供商响应中的电子邮件值作为模型的登录值。

    【讨论】:

      【解决方案2】:

      为了后代:

      此问题已在omniauth-github gem 存储库中解决,地址为this merge

      它在最新版本中不可用,所以如果你想拥有它,你必须通过将 repo 地址添加到你的 gemfile 来获得最前沿的 repo 版本:

      gem 'omniauth-github', :git => "git://github.com/intridea/omniauth-github.git"
      

      【讨论】:

        猜你喜欢
        • 2023-04-05
        • 2012-04-21
        • 1970-01-01
        • 2013-01-11
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多