簡體   English   中英

即使成功通過身份驗證,Devise omniauth也會重定向到回調控制器中的失敗方法。

[英]Devise omniauth is redirecting to failure method in callback controller even after successful authentication.?

我正在設計omniauth以使用Facebook驗證用戶身份。 我已經使用文檔https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview實現了它不能正常工作。 當我使用我的Facebook帳戶登錄時,它將帶我進入回調控制器中的“失敗”方法。 但是有趣的是,當我打開facebook時,它沒有任何問題地打開,意味着它正在驗證我的帳戶。 我已經通過登錄和注銷從Facebook檢查了很多次。 但是問題在於,如果要認證Facebook帳戶,那為什么要帶我去“失敗方法”。 這是我的代碼

回調控制器。

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    # You need to implement the method below in your model (e.g. app/models/user.rb)
    @user = User.from_omniauth(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 => "Facebook") if is_navigational_format?
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end

  def failure
    render plain: params.inspect
   # redirect_to root_path
  end
end

用戶模型

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable
  devise :omniauthable, :omniauth_providers => [:facebook]

def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.email = auth.info.email
      user.password = Devise.friendly_token[0,20]
      user.name = auth.info.name   # assuming the user model has a name
      user.image = auth.info.image # assuming the user model has an image
      # If you are using confirmable and the provider(s) you use validate emails,
      # uncomment the line below to skip the confirmation emails.
      # user.skip_confirmation!
    end
  end

  def self.new_with_session(params, session)
    super.tap do |user|
      if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
        user.email = data["email"] if user.email.blank?
      end
    end
  end
end

路線代碼

devise_for :users,  controllers: {confirmations: 'confirmations',registrations: 'users/registrations',omniauth_callbacks: 'users/omniauth_callbacks' } do
    #put "confirm_user", to: "confirmations#confirm_user"
    get "confirmation", to: "confirmations#after_confirmation_path_for"
  end

Facebook應用設置屏幕截圖為

https://www.dropbox.com/s/vn8cjpr5wyagkdp/Scr​​eenshot%202017-11-27%2017.04.28.png?dl=0

登錄后的開發日志是

Started GET "/users/auth/facebook" for 10.0.2.2 at 2017-11-28 14:13:37 +0000
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Started GET "/users/auth/facebook/callback?code=AQA8oLlPsMLSvZf5NqZfOA0oJxDVKczwqqMIxWPD2dJoucpJl9T7MLTmf0mMDlOhgQPAhKa2f6My4vWGZmTWhVo6S5TbSQ3ELup1iBhDHTb869pMMo-ksa1Kh1gJDG65ZJxUj5vAe8jH-DX0eBRpf_ygZACCoFYFTiZdAIzCXQI7jfLaeqH70CqAffkGZczYzhjThM_NLol3Lzo18ZX_6_5n2-p7nMC3IKhmzDEyo_toyaI1telD3QMwa0re7GIu-UXKV4DQp-ClLT452Bigp9Fhs50wYm-Kl08E7195R2mpBESpB7Gu0moDbCgi61dEEk5u8GGfmm0Cxbu9Fcw1_Eu8" for 10.0.2.2 at 2017-11-28 14:13:40 +0000
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by Users::OmniauthCallbacksController#failure as HTML
  Parameters: {"code"=>"AQA8oLlPsMLSvZf5NqZfOA0oJxDVKczwqqMIxWPD2dJoucpJl9T7MLTmf0mMDlOhgQPAhKa2f6My4vWGZmTWhVo6S5TbSQ3ELup1iBhDHTb869pMMo-ksa1Kh1gJDG65ZJxUj5vAe8jH-DX0eBRpf_ygZACCoFYFTiZdAIzCXQI7jfLaeqH70CqAffkGZczYzhjThM_NLol3Lzo18ZX_6_5n2-p7nMC3IKhmzDEyo_toyaI1telD3QMwa0re7GIu-UXKV4DQp-ClLT452Bigp9Fhs50wYm-Kl08E7195R2mpBESpB7Gu0moDbCgi61dEEk5u8GGfmm0Cxbu9Fcw1_Eu8"}
  Rendering text template
  Rendered text template (0.0ms)
Completed 200 OK in 20ms (Views: 10.6ms | ActiveRecord: 0.0ms)

params.inspect的結果如下

<ActionController::Parameters {"code"=>"AQA8oLlPsMLSvZf5NqZfOA0oJxDVKczwqqMIxWPD2dJoucpJl9T7MLTmf0mMDlOhgQPAhKa2f6My4vWGZmTWhVo6S5TbSQ3ELup1iBhDHTb869pMMo-ksa1Kh1gJDG65ZJxUj5vAe8jH-DX0eBRpf_ygZACCoFYFTiZdAIzCXQI7jfLaeqH70CqAffkGZczYzhjThM_NLol3Lzo18ZX_6_5n2-p7nMC3IKhmzDEyo_toyaI1telD3QMwa0re7GIu-UXKV4DQp-ClLT452Bigp9Fhs50wYm-Kl08E7195R2mpBESpB7Gu0moDbCgi61dEEk5u8GGfmm0Cxbu9Fcw1_Eu8"} permitted: false>

耙路的輸出為

rake routes
                                Prefix Verb     URI Pattern                                                     Controller#Action
                      new_user_session GET      /users/sign_in(.:format)                                        devise/sessions#new
                          user_session POST     /users/sign_in(.:format)                                        devise/sessions#create
                  destroy_user_session DELETE   /users/sign_out(.:format)                                       devise/sessions#destroy
      user_facebook_omniauth_authorize GET|POST /users/auth/facebook(.:format)                                  users/omniauth_callbacks#passthru
       user_facebook_omniauth_callback GET|POST /users/auth/facebook/callback(.:format)                         users/omniauth_callbacks#facebook
       user_twitter_omniauth_authorize GET|POST /users/auth/twitter(.:format)                                   users/omniauth_callbacks#passthru
        user_twitter_omniauth_callback GET|POST /users/auth/twitter/callback(.:format)                          users/omniauth_callbacks#twitter
                     new_user_password GET      /users/password/new(.:format)                                   devise/passwords#new
                    edit_user_password GET      /users/password/edit(.:format)                                  devise/passwords#edit
                         user_password PATCH    /users/password(.:format)                                       devise/passwords#update
                                       PUT      /users/password(.:format)                                       devise/passwords#update
                                       POST     /users/password(.:format)                                       devise/passwords#create
              cancel_user_registration GET      /users/cancel(.:format)                                         users/registrations#cancel
                 new_user_registration GET      /users/sign_up(.:format)                                        users/registrations#new
                edit_user_registration GET      /users/edit(.:format)                                           users/registrations#edit
                     user_registration PATCH    /users(.:format)                                                users/registrations#update
                                       PUT      /users(.:format)                                                users/registrations#update
                                       DELETE   /users(.:format)                                                users/registrations#destroy
                                       POST     /users(.:format)                                                users/registrations#create
                 new_user_confirmation GET      /users/confirmation/new(.:format)                               confirmations#new
                     user_confirmation GET      /users/confirmation(.:format)                                   confirmations#show
                                       POST     /users/confirmation(.:format)                                   confirmations#create
                     new_model_session GET      /models/sign_in(.:format)                                       devise/sessions#new
                         model_session POST     /models/sign_in(.:format)                                       devise/sessions#create
                 destroy_model_session DELETE   /models/sign_out(.:format)                                      devise/sessions#destroy
                    new_model_password GET      /models/password/new(.:format)                                  devise/passwords#new
                   edit_model_password GET      /models/password/edit(.:format)                                 devise/passwords#edit
                        model_password PATCH    /models/password(.:format)                                      devise/passwords#update
                                       PUT      /models/password(.:format)                                      devise/passwords#update
                                       POST     /models/password(.:format)                                      devise/passwords#create
             cancel_model_registration GET      /models/cancel(.:format)                                        devise/registrations#cancel
                new_model_registration GET      /models/sign_up(.:format)                                       devise/registrations#new
               edit_model_registration GET      /models/edit(.:format)                                          devise/registrations#edit
                    model_registration PATCH    /models(.:format)                                               devise/registrations#update
                                       PUT      /models(.:format)                                               devise/registrations#update
                                       DELETE   /models(.:format)                                               devise/registrations#destroy
                                       POST     /models(.:format)                                               devise/registrations#create
                new_model_confirmation GET      /models/confirmation/new(.:format)                              devise/confirmations#new
                    model_confirmation GET      /models/confirmation(.:format)                                  devise/confirmations#show
                                       POST     /models/confirmation(.:format)                                  devise/confirmations#create
               donations_donor_history GET      /donations/donor_history(.:format)                              donations#donor_history
                donations_donor_signup GET      /donations/donor_signup(.:format)                               donations#donor_signup
                                       POST     /donations/donor_signup(.:format)                               donations#donor_signup
                 donations_sms_service POST     /donations/sms_service(.:format)                                donations#sms_service
         donations_create_user_account POST     /donations/create_user_account(.:format)                        donations#create_user_account
            donations_add_user_payroll POST     /donations/add_user_payroll(.:format)                           donations#add_user_payroll
                             donations GET      /donations(.:format)                                            donations#index
                         edit_donation GET      /donations/:id/edit(.:format)                                   donations#edit
                              donation GET      /donations/:id(.:format)                                        donations#show
                                       PATCH    /donations/:id(.:format)                                        donations#update
                                       PUT      /donations/:id(.:format)                                        donations#update
                                       DELETE   /donations/:id(.:format)                                        donations#destroy
                    campaign_donations POST     /campaigns/:campaign_id/donations(.:format)                     donations#create
                 new_campaign_donation GET      /campaigns/:campaign_id/donations/new(.:format)                 donations#new
campaign_donations_create_user_account GET      /campaigns/:campaign_id/donations/create_user_account(.:format) donations#create_user_account
                             campaigns GET      /campaigns(.:format)                                            campaigns#index
                                       POST     /campaigns(.:format)                                            campaigns#create
                          new_campaign GET      /campaigns/new(.:format)                                        campaigns#new
                         edit_campaign GET      /campaigns/:id/edit(.:format)                                   campaigns#edit
                              campaign GET      /campaigns/:id(.:format)                                        campaigns#show
                                       PATCH    /campaigns/:id(.:format)                                        campaigns#update
                                       PUT      /campaigns/:id(.:format)                                        campaigns#update
                                       DELETE   /campaigns/:id(.:format)                                        campaigns#destroy
                         organizations GET      /organizations(.:format)                                        organizations#index
                                       POST     /organizations(.:format)                                        organizations#create
                      new_organization GET      /organizations/new(.:format)                                    organizations#new
                     edit_organization GET      /organizations/:id/edit(.:format)                               organizations#edit
                          organization GET      /organizations/:id(.:format)                                    organizations#show
                                       PATCH    /organizations/:id(.:format)                                    organizations#update
                                       PUT      /organizations/:id(.:format)                                    organizations#update
                                       DELETE   /organizations/:id(.:format)                                    organizations#destroy
          admins_social_sharing_switch POST     /admins/social_sharing_switch(.:format)                         admins#social_sharing_switch
                   admins_error_detail GET      /admins/error_detail(.:format)                                  admins#error_detail
                                       GET      /admins/generate_report/:id(.:format)                           admins#generate_report
                 admins_create_company GET      /admins/create_company(.:format)                                admins#create_company
                                       POST     /admins/create_company(.:format)                                admins#create_company
                 admins_revenue_detail GET      /admins/revenue_detail(.:format)                                admins#revenue_detail
                 admins_create_account GET      /admins/create_account(.:format)                                admins#create_account
                   admins_view_account GET      /admins/view_account(.:format)                                  admins#view_account
                                       GET      /admins/view_company/:id(.:format)                              admins#view_company
              admins_donation_analysis GET      /admins/donation_analysis(.:format)                             admins#donation_analysis
                    admins_link_expiry GET      /admins/link_expiry(.:format)                                   admins#link_expiry
                   admins_edit_profile GET      /admins/edit_profile(.:format)                                  admins#edit_profile
                 admins_update_profile POST     /admins/update_profile(.:format)                                admins#update_profile
                                       POST     /admins/create_account(.:format)                                admins#create_account
                         admin_destroy GET      /admins/:id(.:format)                                           admins#destroy
                                admins GET      /admins(.:format)                                               admins#index
                                       POST     /admins(.:format)                                               admins#create
                             new_admin GET      /admins/new(.:format)                                           admins#new
                            edit_admin GET      /admins/:id/edit(.:format)                                      admins#edit
                                 admin GET      /admins/:id(.:format)                                           admins#show
                                       PATCH    /admins/:id(.:format)                                           admins#update
                                       PUT      /admins/:id(.:format)                                           admins#update
                                       DELETE   /admins/:id(.:format)                                           admins#destroy
                crons_expirylink_alert GET      /crons/expirylink_alert(.:format)                               crons#expirylink_alert
                        users_sign_out GET      /users/sign_out(.:format)                                       devise/sessions#destroy
                                  root GET      /                                                               campaigns#latest

經過2天的努力,我終於自己解決了。 除了寶石本身,其他一切都很好。 如果您只是編寫gem omniauth-facebook ,它將安裝非常舊的1.4.0版本。 我將其更新為'omniauth-facebook', '~> 4.0' ,它的工作方式就像一種魅力。 因此,如果有人遇到同樣的問題,他應該更新gem。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM