OmniAuth railscasts に従っていますが、この問題に直面しています。
Twitter からログインしようとすると表示されるエラー メッセージは次のとおりです。
Validation failed: Password digest can't be blank, Name can't be blank, Email can't be blank, Email is invalid, Password is too short (minimum is 6 characters), Password confirmation can't be blank
私はすでに別の認証を行っており、それと一緒に OmniAuth を機能させようとしています。
Twitter からユーザー名を取得し、それを「名前」に入力したいと思います。ただし、残りの検証をバイパスしたいと思います。したがって、ユーザーが Twitter を使用してサインアップした場合、このユーザーには電子メールもパスワードもありません。
これが私の認証コントローラーです
class AuthenticationsController < InheritedResources::Base
def index
@authentications = current_user.authentications if current_user
end
def create
omniauth = request.env['omniauth.auth']
authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
if authentication
flash[:notice] = "Signed in successfully"
sign_in_and_redirect(:user, authentication.user)
elsif current_user
token = omniauth['credentials'].token
secret = omniauth['credentials'].token
current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'], :token => token, :secret => token_secret)
flash[:notice] = "Authentication successful"
redirect_to authentications_url
else
user = User.new
user.apply_omniauth(omniauth)
if user.save!
flash[:notice] = "Signed in successfully"
sign_in_and_redirect(:user, user)
else
session[:omniauth] = omniauth.except('extra')
redirect_to '/signup'
end
end
end
def destroy
@authentication = current_user.authentications.find(params[:id])
@authentication.destroy
flash[:notice] = "Successfully destroyed authentication"
redirect_to authentications_url
end
end
schema.db
create_table "authentications", :force => true do |t|
t.integer "user_id"
t.string "provider"
t.string "uid"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "secret"
t.string "token"
end
ユーザー.rb
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
has_many :authentications
before_save { |user| user.email = user.email.downcase }
before_save :create_remember_token
validates_uniqueness_of :name, { case_sensitive: false }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }
validates :password_confirmation, presence: true
def apply_omniauth(omni)
authentications.build(:provider => omni['provider'],
:uid => omni['uid'],
:token => omni['credentials'].token,
:secret => omni['credentials'].secret)
end
def twitter
unless @twitter_user
provider = self.authentications.find_by_provider('twitter')
@twitter_user = Twitter::Client.new(:oauth_token => provider.token, :oauth_token_secret => provider.secret) rescue nil
end
@twitter_user
end
def apply_twitter(omniauth)
if (extra = omniauth['extra']['user_hash'] rescue false)
# Example fetching extra data. Needs migration to User model:
# self.firstname = (extra['name'] rescue '')
end
end
def hash_from_omniauth(omniauth)
{
:provider => omniauth['provider'],
:uid => omniauth['uid'],
:token => omniauth['credentials']['token'],
:secret => omniauth['credentials']['secret']
}
end
end
認証コントローラーで使用user.save(:validation => false)
してみましたが、同様のエラーが発生しました。