1

GitGubを介したユーザー認証を許可するようにDevise1.2を設定しようとしています。Cucumberを使用してローカルでテストし、GitHub OAuthをスタブアウトできる限り、正常に機能しているようです。ただし、Herokuにデプロイして認証しようとすると、GitHubからアプリケーションにリダイレクトされた後にエラーが発生します。

Herokuログを確認すると、表示されるエラーは...

PGError:エラー:列「email」のnull値がnot-null制約に違反しています

どうやら、GitHubからメールアドレスが返ってこないのか、途中で失われているのでしょうか。

https://github.com/plataformatec/devise/wiki/OmniAuthの手順と例に従いました:-概要とhttps://github.com/plataformatec/devise/wiki/OmniAuth:--Testing-%27facebook %27-signup-%5BRails-3---Cucumber --- Capybara --- Mongoid-%5Dですが、 https://のGitHub戦略のOmniAuthソースコードを参照して、FacebookではなくGitHub用に変更しました。 github.com/intridea/omniauth/blob/master/oa-oauth/lib/omniauth/strategies/github.rb

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
4

2 に答える 2

0

問題は、GitHubがユーザーIDとしてメールアドレスを使用せず、必ずしもトークンにメール値が含まれていないことです。メールアドレスの代わりにログインを使用して問題を解決しました。

将来的には複数のプロバイダーをサポートする予定なので、テーブルの論理キーとしてフィールドauth_providerとフィールドの組み合わせを使用しており、config/devise.rbファイルで指定しています。ユーザーのログインに電子メールアドレスを使用する別のプロバイダーのサポートを追加する場合、それは問題ありません。モデルのログイン値として、プロバイダーの応答からの電子メール値を使用します。loginusersconfig.authentication_keys = [ :auth_provider, :login ]

于 2011-03-06T04:09:10.057 に答える
0

後世のために:

この問題は、このマージのomniauth-githubgemリポジトリで解決されました。

最新バージョンでは利用できないため、入手したい場合は、gemfileにリポジトリアドレスを追加して、最先端のリポジトリバージョンを入手する必要があります。

gem 'omniauth-github', :git => "git://github.com/intridea/omniauth-github.git"
于 2013-02-24T09:23:29.120 に答える