나는 현재 레일 앱을 개발 중이며 사용자가 정상적으로, 트위터를 통해, 페이스 북을 통해 로그인 할 수 있도록하고있다. 나는 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
class_eval 조언을 주셔서 감사합니다. 당신 말이 맞아요, 옛날 방식은 어색하고 절름발이였습니다. –