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