Omniauth w/ twitter はほとんど機能していますが、twitter の資格情報を検証した後に自分のアプリにリダイレクトしようとすると、次の長いエラーが発生します。
ActiveRecord::StatementInvalid in AuthenticationsController#create
SQLite3::ConstraintException: constraint failed: INSERT INTO "users"
("created_at", "current_sign_in_at", "current_sign_in_ip", "email",
"encrypted_password", "last_sign_in_at", "last_sign_in_ip",
"password_salt", "remember_created_at", "remember_token",
"reset_password_token", "sign_in_count", "updated_at") VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ユーザー.rb:
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
def email_required?
super && :provider.blank?
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 p user 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