[英]Integrating Wicked, Devise and Omniauth-Facebook
我正在使用我在帖子標題中編寫的這三個gem開發一個應用程序。 我使用可確認模塊(?)設置了devise,因此,當用戶使用其電子郵件/密碼創建帳戶時,它將收到確認電子郵件。 如果用戶(使用omniauth-facebook gem)使用facebook進行注冊,則devise跳過確認步驟。
在user.rb中
"Of course the :confirmable is active in the model"
...
# Omniauth-facebook
def self.find_for_facebook_oauth(auth)
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.skip_confirmation!
# user.image = auth.info.image # assuming the user model has an image
end
end
...
當我為向導添加邪惡的寶石時,事情就來了。 我配置了路由文件
在routes.rb中
MyApp::Application.routes.draw do
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks",
:registrations => "registrations" }
root 'home#index'
# Registration wizard routes
resources :after_register
end
我創建了一個registration_controller來覆蓋設計注冊方法
class RegistrationsController < Devise::RegistrationsController
def create
super
end
protected
def after_sign_in_path_for(resource)
puts "<<<<<<<<<<<<<<<< SIGN IN"
after_register_path(:import_contacts)
end
def after_sign_up_path_for(resource)
puts "<<<<<<<<<<<<<<<< SIGN UP ACTIVE"
after_register_path(:import_contacts)
end
def after_inactive_sign_up_path_for(resource)
puts "<<<<<<<<<<<<<<<< SIGN IN INACTIVE"
after_register_path(:import_contacts)
end
end
然后,我創建了一個新的控制器來處理邪惡的向導步驟。
class AfterRegisterController < ApplicationController
include Wicked::Wizard
before_filter :authenticate_user!
steps :import_contacts, :select_agents, :wish_form
def show
@user = current_user
render_wizard
end
def update
@user = current_user
@user.attributes = params[:user]
render_wizard @user
end
end
當我創建一個用戶密碼或密碼時,向導會出現,並且一切正常,但是當我嘗試注冊Facebook時,wizar永遠不會出現。
任何提示?
謝謝!!
如果一切都以通常的方式配置(我所看到的看起來很標准),那么使用Facebook登錄根本就不會通過RegistrationsController
。 它將通過您尚未為其發布代碼的OmniauthCallbacks
控制器進行。 當他們通過Facebook登錄時,這取決於您在那做什么。 我假設您有一個調用User.find_for_facebook_oauth(auth)
的facebook()
方法。 然后,您只是登錄他們而不是去RegistrationsController
嗎? 如果是這樣,那么RegistrationsController
將不會被觸摸,因此其重寫的after_sign_in_path_for
將不起作用。
如果要使覆蓋的after_sign_in_path_for
在整個應用程序中生效,則可以在ApplicationController
對其進行定義。 如果只希望它在OmniauthCallbacksController
,則可以在那里定義它。 無論哪種情況,他們每次使用Facebook登錄時都會被擊中(如果您將其放置在ApplicationController
,則任何人每次使用任何方法登錄時都將被擊中),因此您需要跟蹤他們假設您想確保它僅在他們第一次登錄時就已經通過了向導。如果您正在使用devise:trackable模塊,則可能需要檢查user.sign_in_count,或者您有一些另一種輕松檢查它們是否已經通過向導的方法。
評論問題的更新:
您的第一個問題:“假設我將after_sign_in_path_fot放在ApplicationController中,我應該在RegistrationsController中刪除after_inactive_sign_up_path_for方法,對嗎?” 這取決於您想要的行為。 按照您的問題使用RegistrationsController
,在他們注冊后並且確認電子郵件之前,它將進入向導(因為在這種情況下將調用after_inactive_sign_up_path_for
)。 當他們確認並登錄后, ApplicationController
的after_sign_in_path_for
將再次將其發送給向導。 因此,是的,如果您只想在登錄后使用向導,則從RegistrationsController
刪除不活動的對象。然后在RegistrationsController
可能after_sign_up_path_for(resource)
,因為after_sign_in_path_for(resource)
的默認實現只是調用了您將在ApplicationController
after_sign_in_path_for(resource)
。 無論如何,由於RegistrationsController.create()
的默認實現中的邏輯,如果總是需要確認,將不會調用after_sign_up_path_for()
-要求確認將導致resource.active_for_authentication?
返回false會導致after_inactive_sign_up_path_for(resource)
被調用。
對於第二條評論中的問題,您說“如果我在ApplicationController中刪除after_sign_in_path_for”,則認為您的意思是RegistrationsController
? 如果是正確的話,那么是的,在您的重寫版本的RegistrationsController
不需要任何方法(如果這是您在問題中粘貼的整個控制器),因為您的create()
只是調用super
, after_sign_in_path_for
將在ApplicationController
,您可能不after_sign_in_path_for
不需要上述after_(inactive)_sign_up_path_for
方法中的任何一個。 因此,是的,不需要您的RegistrationsController
。 您可以將其完全刪除,然后在route.rb中刪除:registrations => "registrations"
-然后,將再次使用RegistrationsController
的設計實現。
然后,您說“僅在ApplicationController中覆蓋RegistrationsController的方法?”。 您從RegistrationsController
剩下的唯一方法是ApplicationController
的after_sign_in_path_for(resource)
,因此,我認為RegistrationsController
中沒有其他需要在ApplicationController
。 讓我知道我是否錯過了您的要求之一或做出了錯誤的假設。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.