简体   繁体   English

如何使用 omniauth 查看用户的个人数据?

[英]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.我想通过 Ruby on Rails 使用 gem Omniauth 查看用户的个人数据。 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

You will need to request a specific scope on your GitHub omniauth configuration.您需要在 GitHub omniauth 配置上请求特定范围。

config/initializers/devise.rb配置/初始化程序/devise.rb

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从omniauth保存用户的Facebook数据 - Saving user's Facebook data from omniauth 在使用OmniAuth时,如何指定用户的Facebook帐户需要哪些访问权限? - How can I specifiy what access I need from my user's Facebook accounts when using OmniAuth? 使用Rails Omniauth gem和Google OpenID时,如何不要求用户发送电子邮件 - How do I NOT require user's email when using Rails Omniauth gem and Google OpenID 如何为登录编写Omniauth RSpec? - How can I write an Omniauth RSpec for the login? Rails 3 + Omniauth。 当用户单击FB的“不允许”时,如何自定义响应 - Rails 3 + Omniauth. When a user clicks “Don't Allow” for FB, how can I customize the response 如何使用Omniauth gem登录用户以进行我的Rails测试? - How can I login a user for my Rails tests using Omniauth gem? 如何使用OmniAuth允许每个用户几个Instagram身份验证? - How do I allow several Instagram authentications per user with OmniAuth? 如何确定Rails中正在使用哪个身份验证用户(OmniAuth) - How do I tell which authentication user is using in rails (OmniAuth) 我怎样才能再有一个范围来omniauth帐户? - How I can one more scopes to omniauth account? 没有设计的OmniAuth:如何安全地实现“记住我”? - OmniAuth without Devise: How can I securely implement Remember Me?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM