2

我正在使用 Ruby on Rails 构建一个 Web 应用程序,我希望我的用户能够验证和聚合来自 Linked In(以及 Github、Twitter 等其他人)的数据。

我正在使用这些宝石:

  • 设计注册
  • 用于身份验证的omniauth-linkedin
  • pengwynn/linkedin 用于数据聚合

不过,Linked In 有一个不太好的引脚。有没有办法避免它并从我的用户帐户中获取我想要的数据,而无需他们去链接,获取 pin 并将其提交给我?

提前致谢。

authentications_controller.rb

class AuthenticationsController < ApplicationController
  def index
    @authentications = current_user.authentications if current_user
  end

  def create
    omniauth = request.env["omniauth.auth"]
    authentication = Authentication.
          find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
    if authentication
      flash[:notice] = "Signed in successfully."
      sign_in_and_redirect(:user, authentication.user)
    elsif current_user
      current_user.authentications.
            create!(provider: omniauth['provider'], uid: omniauth['uid'])
      current_user.apply_omniauth(omniauth)
      flash[:notice] = "Authentication successful."
      redirect_to authentications_url
    else
      user = User.new
      user.apply_omniauth(omniauth)
      if user.save
        flash[:notice] = "Signed in successfully."
        sign_in(:user, user)
        redirect_to user_path(user.username)
      else
        session[:omniauth] = omniauth.except('extra')
        redirect_to new_user_registration_url
      end
   end
end
4

0 回答 0