0

Can i get in params twitter email using devise :omniauthable?

I can get facebook email and create user with this method:

def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
  data = access_token['extra']['user_hash']
  if user = User.find_by_email(data["email"])
    user
  else # Create a user with a stub password.
    User.create!(:email => data["email"], :password => Devise.friendly_token[0,20])
  end 
end

controller:

class OmniauthCallbacksController < Devise::OmniauthCallbacksController

  def facebook
    @user = User.find_for_facebook_oauth(env["omniauth.auth"], current_user)
    if @user.persisted?
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
      sign_in_and_redirect @user, :event => :authentication
    else
      session["devise.facebook_data"] = env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end

  def twitter
    @user = User.find_for_twitter_oauth(env['omniauth.auth'])

    if @user.persisted?
      flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', :kind => 'Twitter'
      sign_in_and_redirect @user, :event => :authentication
    else
      flash[:notice] = I18n.t 'devise.omniauth_callbacks.failure', :kind => 'Twitter', :reason => 'User not found'
      redirect_to new_user_session_path
    end
  end
end

if i using something like this for twitter oauth in incoming params i got user_id, uid, credentials, secret, etc... but there is no email. Can I get it using this way? I don't want add new fields for users table or add new table with user ids, oauth prowiders, and oauth ids.

Mb more cleanest way exist with warden strategy for twitter?

dom1nga
  • 167
  • 1
  • 7

1 Answers1

2

the Twitter API DOES NOT return emails, so its up to you how to handle that

daniel
  • 901
  • 1
  • 10
  • 21