所以我的Omniauth的Twitter验证功能大约有95%,换句话说,它几乎是全功能的。单击我的应用程序上的|使用Twitter登录|按钮时,它会将我重定向到twitter,然后提示我输入Twitter凭据,然后重定向回该应用程序。
但是,在Twitter身份验证过程之后,我没有登录,而是在登录页面上收到以下错误:
1 error prohibited this user from being saved:
Email can't be blank如何让Omniauth将我重定向到登录页面,在我的例子中是/posts页面?当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发布于 2013-03-01 04:45:28
我刚想起来,原因其实很简单,twitter不会在omniauth请求中返回电子邮件,所以它会将你重定向到新用户注册页面,你必须填写你的电子邮件。
https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema
此外,这可能会有所帮助。
https://stackoverflow.com/questions/15145065
复制相似问题