Module: NimbleAuth::Omniauth

Defined in:
lib/nimble_auth/omniauth.rb,
app/services/nimble_auth/omniauth/facebook.rb,
app/services/nimble_auth/omniauth/google_oauth2.rb

Defined Under Namespace

Classes: Facebook, GoogleOauth2

Class Method Summary collapse

Class Method Details

.append_omniauth_callback_path(provider) ⇒ Object

Appends the ‘callback_path` to oauth provider strategy.

This is the path user will be redirected to upon successful login.

Eg: for facebook,

sets callback_path to match `user_facebook_omniauth_callback_path`


40
41
42
43
44
45
# File 'lib/nimble_auth/omniauth.rb', line 40

def append_omniauth_callback_path(provider)
  provider_path = "user_#{provider}_omniauth_callback_path"
  provider_strategy = devise_provider_strategy(provider)

  provider_strategy[:callback_path] = auth_url_helpers.send(provider_path)
end

.auth_url_helpersObject



47
48
49
# File 'lib/nimble_auth/omniauth.rb', line 47

def auth_url_helpers
  NimbleAuth::Engine.routes.url_helpers
end

.configure_providersObject



5
6
7
8
9
10
11
12
13
# File 'lib/nimble_auth/omniauth.rb', line 5

def configure_providers
  # Refresh the loaded routes with appended omninauth paths
  Rails.application.reload_routes!

  NimbleAuth.configuration.omniauth_providers.each do |provider|
    generate_omniauth_path_helpers(provider)
    append_omniauth_callback_path(provider)
  end
end

.devise_provider_strategy(provider) ⇒ Object



51
52
53
# File 'lib/nimble_auth/omniauth.rb', line 51

def devise_provider_strategy(provider)
  Devise.omniauth_configs[provider].strategy
end

.generate_omniauth_path_helpers(provider) ⇒ Object

Generates url helpers for oauth providers. Adds the helpers to ‘Engine` helpers to make it accessible in other parts of the app.

Helper Format: [:provider]_authorize_path

Eg: facebook_authorize_path, instagram_authorize_path


21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/nimble_auth/omniauth.rb', line 21

def generate_omniauth_path_helpers(provider)
  NimbleAuth::Engine.helpers.module_eval do
    define_method "#{provider}_authorize_path" do |*arg|
      query_params = arg.first
      omniauth_provider_path = "#{OmniAuth.config.path_prefix}/#{provider}"

      return omniauth_provider_path.to_s if query_params.blank?

      "#{omniauth_provider_path}?#{query_params.to_query}"
    end
  end
end