设计omniauth(oauth)sign_in和sign_up路径设置

时间:2016-09-05 18:50:10

标签: ruby-on-rails oauth devise

我想将我的oauth重定向路径分开。一个用于facebook和google(register/info1),另一个用于twitter(register/info2)。

  1. 我的用户在第一次注册时会转到register/info(1/2)页面。
  2. 用户注册并在下次登录后,会转到home/main,而不是register/info(1/2)

    after_sign_up_path_for方法无效omniauth_callback_controller。因此,用户总是转到register/info(1/2)。 目前,我将omniauth_callback_controllers.rb设置为这样。

  3.   

    class User :: OmniauthCallbacksController<   设计:: OmniauthCallbacksController

       def self.provides_callback_for(provider)
         class_eval %Q{
           def #{provider}
             @user = User.find_for_oauth(env["omniauth.auth"], current_user)
             if @user.persisted?
               sign_in_and_redirect @user, event: :authentication
             else
               session["devise.#{provider}_data"] = env["omniauth.auth"]
               redirect_to new_user_registration_url
             end
           end
          }
      end
    
    
      [:twitter, :facebook, :google_oauth2].each do |provider|
        provides_callback_for provider
      end
    
    
    
      def after_sign_in_path_for(resource)
          auth = request.env['omniauth.auth'] 
          @identity = Identity.find_for_oauth(auth)
    
          @user = User.find(current_user.id)
          if @user.persisted?
            if @identity.provider == "twitter"
              register_info2_path
            else
              register_info1_path
            end
          else
            home_main_path
          end
      end
    

    如果您有任何想法或相关文件,请告诉我。感谢。

1 个答案:

答案 0 :(得分:0)

您可以在omniauth.rb初始化程序中设置每个策略的回调路径:

例如:

Rails.application.config.middleware.use OmniAuth::Builder do
    provider :facebook, "app_id", "password",
        scope: 'public_profile', info_fields: 'id,name,link', display: 'popup', callback_path: '/stores/facebook_callback'

end