というわけで、Omniauth の Twitter 検証機能は約 95% で機能しています。つまり、ほぼ完全に機能しています。|をクリックすると Twitter でサインイン | アプリのボタンをクリックすると、Twitter にリダイレクトされ、Twitter の資格情報を入力するように求められ、アプリにリダイレクトされます。
ただし、Twitter 認証プロセスの後にログインする代わりに、サインイン ページに次のエラーが表示されます。
1 error prohibited this user from being saved:
Email can't be blank
/posts ページの場合、Omniauth にログイン済みページにリダイレクトさせるにはどうすればよいですか? また、Omniauth が Twitter 検証を介して私を承認することになっているのに、なぜこのようなエラーが発生するのでしょうか?
ユーザーモデル:
class User < ActiveRecord::Base
has_many :authentications
# Include default devise modules. Others available are:
# :token_authenticatable, :lockable, :timeoutable and :activatable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation
# attr_accessible :title, :body
def apply_omniauth(omniauth)
self.email = omniauth['info']['email'] if email.blank?
authentications.build(:provider => omniauth['provider'], :uid => omniauth['uid'])
end
def password_required?
(authentications.empty? || !password.blank?) && super
end
end
認証コントローラー:
class AuthenticationsController < ApplicationController
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
current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'])
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 new_user_registration_url
end
end
end
end
登録管理者:
class RegistrationsController < Devise::RegistrationsController
def create
super
session[:omniauth] = nil unless @user.new_record?
end
private
def build_resource(*args)
super
if session[:omniauth]
@user.apply_omniauth(session[:omniauth])
@user.valid?
end
end
end