retrograde
retrograde

Reputation: 51

How can I view user's personal data with omniauth?

I want to view a user's personal data with the gem Omniauth with Ruby on Rails. Right now, I can only view public data. I get an error email can't be blank because I can only access public data, rather than personal user data. All answers are appreciated. Here's the code for my application:

#identity.rb

class Identity < ActiveRecord::Base
 belongs_to :user
 validates_presence_of :uid, :provider
 validates_uniqueness_of :uid, :scope => :provider

 def self.find_for_oauth(auth)
   find_or_create_by(uid: auth.uid, provider: auth.provider)
 end

end

#user.rb


      def self.find_for_oauth(auth, signed_in_resource = nil)
identity = Identity.find_for_oauth(auth)
user = signed_in_resource ? signed_in_resource : identity.user
if user.nil?

  email = auth.info.email
  user = User.where(:email => email).first if email

  # Create the user if it's a new registration
  if user.nil?
    user = User.new(
      full_name: auth.info.name,
      username: 'User'+auth.uid,
      email: auth.info.email,
      password: auth.uid
    )
    user.skip_confirmation!
    user.save!
   end 
end


if identity.user != user
  identity.user = user
  identity.save!
end
user
 end

#omniauth_callbacks_controller.rb


     def self.provides_callback_for(provider)
class_eval %Q{
  def #{provider}
    @user = User.find_for_oauth(env["omniauth.auth"], current_user)

    if @user.persisted?
      sign_in_and_redirect @user, event: :authentication
      set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format?
    else
      session["devise.#{provider}_data"] = env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
}
 end

[:github, :google_oauth2].each do |provider|
provides_callback_for provider
 end

Upvotes: 2

Views: 271

Answers (1)

JeffD23
JeffD23

Reputation: 9318

You will need to request a specific scope on your GitHub omniauth configuration.

config/initializers/devise.rb

config.omniauth :github, 'YOUR_API_KEYS', scope: 'user'

Upvotes: 1

Related Questions