Rails 4使用Devise和Omniauth进行身份验证

时间:2014-06-09 06:15:29

标签: ruby-on-rails devise omniauth

我已经完成了30多个博客,我担心我还没有发现我所犯的错误步骤或错误。

我正在尝试使用我的Rails 4应用程序设置LinkedIn身份验证。它使用devise和omniauth(使用omniauth-linkedin-oauth2)。

我的用户模型包含:

  def self.find_for_linkedin_oauth(auth)
   puts auth.info 
   where(auth.slice(:provider, :uid)).first_or_create do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.email = auth.info.email
      user.password = Devise.friendly_token[0,20]
      user.first_name = auth.info.first_name  
      user.last_name = auth.info.last_name   
      user.image = auth.info.image 
    end
  end

我有omniauth.rb初始化程序:

 Rails.application.config.middleware.use OmniAuth::Builder do
   provider :linkedin, "key", "secret",
     REDIRECT_URI = 'http://www.homepage.com' #Redirect users after authentication to this path, ensure that you have set up your routes to handle the callbacks
     STATE = [long tricky code] #A unique long string that is not easy to guess
     :scope => "r_basicprofile r_emailaddress", 
     :field => ["id", "email-address", "first-name", "last-name" ],
     :client_options => {:ssl => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'}} 
 end

我有一个omniauth_callbacks控制器:

 def linkedin
    @user = User.find_for_linkedin_oauth(request.env["omniauth.auth"])
      if @user.persisted?
         sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
          set_flash_message(:notice, :success, :kind => "LinkedIn") if is_navigational_format?
        else
          session["devise.linkedin_data"] = request.env["omniauth.auth"]
          redirect_to root_path
        end
      end

我有Facebook设置和工作。不幸的是,我发现了一篇博文,我可以在没有学习Facebook的情况下关注,所以我发现很难看到如何设置它。我还没有理解发现任何错误所涉及的代码。任何帮助将非常感激。谢谢。

0 个答案:

没有答案