パルカワ2

最近はFlutterをやっています

Rails+devise+omniauthでFacebook認証する前に特定のアクションを実行したい

omniauthのバージョンは、1.1.4

https://github.com/intridea/omniauth/blob/master/lib/omniauth/strategy.rb#L298

# The setup phase looks for the `:setup` option to exist and,
# if it is, will call either the Rack endpoint supplied to the
# `:setup` option or it will call out to the setup path of the
# underlying application. This will default to `/auth/:provider/setup`.
def setup_phase
  if options[:setup].respond_to?(:call)
    log :info, "Setup endpoint detected, running now."
    options[:setup].call(env)
  elsif options.setup?
    log :info, "Calling through to underlying application for setup."
    setup_env = env.merge('PATH_INFO' => setup_path, 'REQUEST_METHOD' => 'GET')
    call_app!(setup_env)
  end
end

setup_phaseというのがあるのでそれを使う。

# config/なんちゃらに追加
module OmniAuth
  module Strategies
    class Facebook
      configure do |c|
        # deviseを使っていると /resource_name/auth/facebook/setup が実行される
        c.setup = true
      end
    end
  end
end

あとはよしなにルーティングとアクションとビューを追加する。ただcallbackの時もこのアクションが実行されると思うので注意
もっといい方法あるなら知りたい。