首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Omniauth重定向

Omniauth重定向
EN

Stack Overflow用户
提问于 2013-03-01 04:29:54
回答 1查看 910关注 0票数 1

所以我的Omniauth的Twitter验证功能大约有95%,换句话说,它几乎是全功能的。单击我的应用程序上的|使用Twitter登录|按钮时,它会将我重定向到twitter,然后提示我输入Twitter凭据,然后重定向回该应用程序。

但是,在Twitter身份验证过程之后,我没有登录,而是在登录页面上收到以下错误:

代码语言:javascript
复制
1 error prohibited this user from being saved:

    Email can't be blank

如何让Omniauth将我重定向到登录页面,在我的例子中是/posts页面?当Omniauth应该通过Twitter验证来授权我时,为什么它会产生这样的错误?

用户模型:

代码语言:javascript
复制
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

身份验证控制器:

代码语言:javascript
复制
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

注册控制器:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-01 04:45:28

我刚想起来,原因其实很简单,twitter不会在omniauth请求中返回电子邮件,所以它会将你重定向到新用户注册页面,你必须填写你的电子邮件。

https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema

此外,这可能会有所帮助。

Skip email validation for omniauth-twitter on devise 2.0

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15145065

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档