Sha256: 2aa4082c8c4d3c806445c88517198247d0049f74e1795889ade6b756deb43157

Contents?: true

Size: 1.94 KB

Versions: 2

Compression:

Stored size: 1.94 KB

Contents

<% module_namespacing do -%>
class <%= class_name.pluralize %>::OmniauthCallbacksController < Devise::OmniauthCallbacksController

<% providers.each do |provider| -%>
  def <%= provider.underscore %>
    create
  end

<% end -%>
  private

    def create
      auth_params = request.env["omniauth.auth"]
      provider = AuthenticationProvider.where(name: auth_params.provider).first
      authentication = provider.<%= class_name.downcase %>_authentications.where(uid: auth_params.uid).first
      existing_<%= class_name.downcase %> = current_<%= class_name.downcase %> || <%= class_name %>.where('email = ?', auth_params['info']['email']).first

      if authentication
        sign_in_with_existing_authentication(authentication)
      elsif existing_<%= class_name.downcase %>
        create_authentication_and_sign_in(auth_params, existing_<%= class_name.downcase %>, provider)
      else
        create_<%= class_name.downcase %>_and_authentication_and_sign_in(auth_params, provider)
      end
    end

    def sign_in_with_existing_authentication(authentication)
      sign_in_and_redirect(:<%= class_name.downcase %>, authentication.<%= class_name.downcase %>)
    end

    def create_authentication_and_sign_in(auth_params, <%= class_name.downcase %>, provider)
      <%= class_name %>Authentication.create_from_omniauth(auth_params, <%= class_name.downcase %>, provider)

      sign_in_and_redirect(:<%= class_name.downcase %>, <%= class_name.downcase %>)
    end

    def create_<%= class_name.downcase %>_and_authentication_and_sign_in(auth_params, provider)
      <%= class_name.downcase %> = <%= class_name %>.create_from_omniauth(auth_params)
      if <%= class_name.downcase %>.valid?
        create_authentication_and_sign_in(auth_params, <%= class_name.downcase %>, provider)
      else
        flash[:error] = <%= class_name.downcase %>.errors.full_messages.first
        redirect_to new_<%= class_name.downcase %>_registration_url
      end
    end
end
<% end -%>

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
domp-0.0.7 lib/generators/domp/templates/omniauth_callbacks_controller.rb
domp-0.0.6 lib/generators/domp/templates/omniauth_callbacks_controller.rb