18

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!

hyperrjas
  • 10,666
  • 25
  • 99
  • 198

4 Answers4

29

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.

Dan Stark
  • 291
  • 3
  • 3
  • Same issue here, have imlpemented your code to my user.rb but i'm getting the following error: undefined local variable or method `provider' for #. the provider method isn't defined anywhere in the user.rb? –  Feb 28 '13 at 21:06
  • @TonyP Do you have a `provider` attribute on your User model? – zykadelic Jun 21 '13 at 09:29
  • Could you add a little more explanation what it does? If there are hand written validation rules in user.rb for password and email, does it work either? For me not :( – Gediminas Šukys Jul 19 '14 at 04:06
2

I am facing the same problem, Twitter doesn't give you the email via oauth, you have to think in another way to obtain the email. The only solution that I figured out, is override the callback and use the twitter data to autocomplete a form and give the user the chance to complete his mail by himself and then sign up and save the user in the database.

Look the following question:

Is there a way to get an user's email ID after verifying his/her Twitter identity using OAuth?

Community
  • 1
  • 1
McSas
  • 2,224
  • 3
  • 27
  • 42
2

A solution is explained thoroughly here: http://asciicasts.com/episodes/236-omniauth-part-2

And on GitHub: https://github.com/fertapric/rails3-mongoid-devise-omniauth/wiki/How-To:-Retrieve-email-information-(middle-step)-from-providers-like-Twitter-or-LinkedIn

For some reason, this question shows up higher in Google's ranks than these links, so I thought I'd add them to the answers.

Christopher Davies
  • 4,461
  • 2
  • 34
  • 33
0

You need to remove the presence validation of the email field. Follow this post and see if that works. Sorry for the late response, hope this helps.

Jack Weiss
  • 19
  • 3
  • 3
    Thank you jacobaweiss, but I need get a email from user. I can not save in my database a user without his email :(. Perhaps I need create a redirect and show a form and user must write the email for create his account. How can I do it? Thank you – hyperrjas May 01 '12 at 08:55
  • I think you need to force the user to some form to fill the information, but I'm searching how to do that either :/ – CanCeylan Nov 08 '12 at 18:40