class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController Devise.omniauth_configs.keys.each do |provider| define_method(provider.to_s) { omniauth_callback(provider) } end def failure redirect_to root_path end private def omniauth_callback(provider) auth_data = request.env["omniauth.auth"] user = SigninWithOmniauth.new(auth_data).execute if user.present? && user.persisted? sign_in_and_redirect user, event: :authentication set_flash_message(:notice, :success, kind: provider.to_s.capitalize) if is_navigational_format? else session["devise.oauth_data"] = auth_data.except(:extra) redirect_to new_user_session_path end end end