本文介绍了在devise 2.0上跳过omniauth-twitter的电子邮件验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用devise 2.0和gem



问题是,twitter不会发送电子邮件,所以我的应用程序的模型用户验证有一封电子邮件,并在回调中收到以下错误:

 电子邮件不能为空

我有这在我的 user.rb 模型中:

  #config omniauth twitter 
def self。 find_for_twitter_oauth(access_token,signed_in_resource = nil)
data = access_token.extra.raw_info
如果user = User.where(:username => data.screen_name).first
user
else
User.create!(:username => data.screen_name,:password => Devise.friendly_token)
end
end
/ pre>

我有这个在我的 omniauth_callbacks_controller.rb

  def twitter 
@user = User.find_for_twitter_oauth(request.env [omniauth.auth],current_user)
如果@ user.persisted?
flash [:notice] = I18n.tdevise.omniauth_callbacks.success,:kind => Twitter
sign_in_and_redirect @user,:event => :身份验证
else
#http://stackoverflow.com/questions/7117200/devise-for-twitter-cookie-overflow-error
会话[devise.twitter_data] =请求。 env [omniauth.auth]。except('extra')
redirect_to new_user_registration_url
end
end

我想要强制给用户发送电子邮件地址发送通讯,广告等...



我如何解决这个问题?



谢谢!

解决方案

将其添加到您的用户模型中:

  def email_required? 
超级&& provider.blank?
end

您可以使用等效的密码方法:

  def password_required? 
超级&& provider.blank?
end

使用Omniauth提供程序时,这应该覆盖字段的要求。


I'm using devise 2.0 and gem omniauth-twitter

The problem is that twitter does not send an email in response, so the model user of my application validates that there is an email and I get the following error in the callback:

Email can't be blank

I have this in my user.rb model:

#config omniauth twitter
def self.find_for_twitter_oauth(access_token, signed_in_resource = nil)
    data = access_token.extra.raw_info
    if user = User.where(:username => data.screen_name).first
        user
    else
        User.create!(:username => data.screen_name, :password => Devise.friendly_token)
    end
end

and I have this in my omniauth_callbacks_controller.rb

  def twitter
    @user = User.find_for_twitter_oauth(request.env["omniauth.auth"], current_user)
    if @user.persisted?
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Twitter"
      sign_in_and_redirect @user, :event => :authentication
    else
      # http://stackoverflow.com/questions/7117200/devise-for-twitter-cookie-overflow-error
      session["devise.twitter_data"] = request.env["omniauth.auth"].except('extra')
      redirect_to new_user_registration_url
    end
   end

I want force to user to give a email address for send newsletter, advertirser...etc

How can I fix this problem?

Thank you!

解决方案

Add this to your User model:

def email_required?
  super && provider.blank?
end

You can use an equivalent method for the password:

def password_required?
  super && provider.blank?
end

This should override the field requirements when using Omniauth providers.

这篇关于在devise 2.0上跳过omniauth-twitter的电子邮件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 11:59