2014-08-29 1 views
0

나는 현재 레일 앱을 개발 중이며 사용자가 정상적으로, 트위터를 통해, 페이스 북을 통해 로그인 할 수 있도록하고있다. 나는 devise, omniauth-twitter 및 omniauth-facebook을 사용하고 있습니다.Devise 's after_sign_in_path_for는 정상적으로 로그인 한 후 작동하지만 Facebook 또는 Twitter를 통해 로그인 한 후에는 작동하지 않습니다. 왜 그런가요?

가입/등록 후 사용자를 verify_user_email_path 페이지로 리디렉션하고 싶습니다. 표준 devise RegistrationsController를 재정의하는 내 사용자 정의 devrs RegistrationsController가 있습니다. 그것은 다음과 같습니다 당신이 볼 수 있듯이

class CustomDeviseControllers::RegistrationsController < Devise::RegistrationsController 
    def verify_email 
    flash[:notice] = "" 
    end 

    def update_email 
    @user = User.find(current_user.id) 
    params[:user].delete(:current_password) 
    if @user.update_without_password(devise_parameter_sanitizer.sanitize(:account_update)) 
     flash[:notice] = "Welcome! You have signed up successfully." 
     sign_in @user, :bypass => true 
     redirect_to bookshelf_index_url 
    else 
     @error_message = "You have entered an invalid email address" 
     render "verify_email" 
    end 
    end 

    protected 
    def after_sign_up_path_for(resource) 
     # I WANT TO REDIRECT USERS HERE AFTER SIGNING UP 
     verify_user_email_path 
    end 
end 

는 최대 어떤 사용자가 로그인 한 후, 그들은 verify_user_email_path로 리디렉션해야합니다. 그러나/슬프게도 사용자가 정상적으로 가입하는 경우에만 리디렉션됩니다. 따라서 사용자가 내 사이트에 등록 할 때 (이메일, 비밀번호를 입력 한 다음 비밀번호를 확인하면) after_sign_up_path_for은 사용자를 verify_user_email_path으로 올바르게 리디렉션합니다. 사용자가 Facebook/Twitter를 통해 가입하는 경우 사용자는 대신 root_url로 리디렉션됩니다. 그게 내가 고쳐야 할 버그 야.

여기
class CustomDeviseControllers::OmniauthCallbacksController < Devise::OmniauthCallbacksController 
    def self.provides_callback_for(provider) 
    class_eval %Q{ 
     def #{provider} 
     @user = User.from_omniauth(request.env["omniauth.auth"]) 

     if @user.persisted? 
      # HERE IS WHERE I SIGN USERS IN 
      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 

    [:twitter, :facebook].each do |provider| 
    provides_callback_for provider 
    end 
end 

Aaaand, 내 설정/routes.rb입니다 :

Rails.application.routes.draw do 
    devise_for :users, :controllers => { 
    omniauth_callbacks: 'custom_devise_controllers/omniauth_callbacks', 
    registrations: 'custom_devise_controllers/registrations' } 

    devise_scope :user do 
    get "users/verify_email" => 'custom_devise_controllers/registrations#verify_email', :as => :verify_user_email 
    post "users/update_email" => 'custom_devise_controllers/registrations#update_email', :as => :update_user_email 
    end 

답변

0

귀하의 OmniauthCallbacksController (모두 대문자 주석 내가의 사용자 서명 어디 아래)

내 사용자 정의 OmniauthCallbacksController입니다 방법 after_sign_up_path_for을 사용하지 않으므로 원하는 리다이렉션을 얻지 못한다.

또한 class_eval 것은 완전히 불필요합니다. 여기에 해당 컨트롤러가 보일 것입니다 방법은 다음과 같습니다

class CustomDeviseControllers::OmniauthCallbacksController < Devise::OmniauthCallbacksController 
    def all 
    omniauth = env["omniauth.auth"] 
    @user = User.from_omniauth(omniauth) 
    if @user.persisted? 
     sign_in @user 
     set_flash_message(:notice, :success, kind: omniauth.provider.capitalize) if is_navigational_format? 
     redirect_to verify_user_email_path 
    else 
     session["devise.#{omniauth.provider}_data"] = omniauth 
     redirect_to new_user_registration_url 
    end 
    end 

    alias_method :facebook, :all 
    alias_method :twitter, :all 
end 
+0

class_eval 조언을 주셔서 감사합니다. 당신 말이 맞아요, 옛날 방식은 어색하고 절름발이였습니다. –

1

대신 내 등록 컨트롤러에서 after_sign_up_path_for 사용하여, 나는 응용 프로그램 컨트롤러 after_sign_in_path_for 사용합니다. 로그인 시도 횟수가 하나 인 경우 사용자가 방금 등록했다는 의미입니다.

클래스와 ApplicationController < ActionController :: 자료와 protect_from_forgery : : : 이제 예외

def after_sign_in_path_for(resource) 
    if resource.sign_in_count == 1 
     verify_user_email_path 
    else 
     root_url 
    end 
    end 
end 

, 사람이 첫 번째 징후가, 그들이 verify_user_email_path 갈 때마다

여기 내 application_controller.rb입니다. 다른 시간에 로그인하면 root_url로 이동합니다.

감사합니다.

관련 문제