kambi
kambi

Reputation: 3423

Devise Omniauth undefined method omniauth_authorize_path

I've noticed that when logging to Devise I have started to receive these error message.

I'm using Devise 2.2.4 with Omniauth 1.1.4 and Omniauth-Facebook 1.4.1

Does anybody know what is the cause of this error?

  ActionView::Template::Error (undefined method `omniauth_authorize_path' for #<#<Class:0xb85e534>:0xb904e5c>):
21: <%- if devise_mapping.omniauthable? %>
22:   <%- resource_class.omniauth_providers.each do |provider| %>
23:     <% logger.info "hey #{provider} , dolphin and #{resource_name}" %>
24:     <%= link_to "Sign in with #{provider.to_s.titleize}", omniauth_authorize_path(resource_name, provider) %><br />
25:   <% end -%>
26: <% end -%>

  app/views/devise/shared/_links.erb:24:in `block in _app_views_devise_shared__links_erb___1039642231_94147460'
  app/views/devise/shared/_links.erb:22:in `each'
  app/views/devise/shared/_links.erb:22:in `_app_views_devise_shared__links_erb___1039642231_94147460'
  app/views/devise/sessions/new.html.erb:17:in `_app_views_devise_sessions_new_html_erb__883448937_92868060'

Upvotes: 16

Views: 12260

Answers (8)

al .js
al .js

Reputation: 206

Super old post, but wanted to possibly help someone in a similar situation.

If you're using OmniAuth with multiple models, none of these answers will work, and you should fall back to the basic OmniAuth way of just redirecting users to /auth/:provider:

To use OmniAuth, you need only to redirect users to /auth/:provider, where :provider is the name of the strategy (for example, developer or twitter). From there, OmniAuth will take over and take the user through the necessary steps to authenticate them with the chosen strategy.

In my case with Okta, /auth/okta worked as long as I properly populated client_options with omniauth-okta.

Hope this helps anyone else dealing with Devise + OmniAuth + Okta!

Upvotes: 0

Yaroslav
Yaroslav

Reputation: 151

If you initialize your devise providers in config/initializers/omniauth.rb You should include Devise::OmniAuth::UrlHelpers in your config/initializers/omniauth.rb or config/initializers/devise.rb

Upvotes: 3

Rupali
Rupali

Reputation: 321

In your app/views/devise/shared/_links.erb :

change

omniauth_authorize_path

to

user_omniauth_authorize_path(provider)

Upvotes: 1

Aeramor
Aeramor

Reputation: 330

Devise changed the url helper to be omniauth_authorize_path(<scope>, <provider>)

See here: http://www.rubydoc.info/github/plataformatec/devise/Devise%2FOmniAuth%2FUrlHelpers%3Aomniauth_authorize_path

Upvotes: 2

Riiwo
Riiwo

Reputation: 1929

Do it like that

<%- if devise_mapping.omniauthable? %>
  <%- resource_class.omniauth_providers.each do |provider| %>
    <%= link_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", public_send("user_#{provider.to_s}_omniauth_authorize_path") %><br />
  <% end -%>
<% end -%>

This makes it usable for multiple providers but it assumes you are using

devise_for :users

But going even further you could also add

resource_class.name.downcase

to cover not only user

<%- if devise_mapping.omniauthable? %>
  <%- resource_class.omniauth_providers.each do |provider| %>
     <%= link_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", public_send("#{resource_class.name.downcase}_#{provider.to_s}_omniauth_authorize_path") %><br />
  <% end -%>
<% end -%>

If devise_for is users and provider is facebook, then it will make path:

user_facebook_omniauth_authorize_path

if devise_for is admins and provider twitter, then it will make path:

admin_twitter_omniauth_authorize_path

Upvotes: 3

Ashitaka
Ashitaka

Reputation: 19193

Try

user_omniauth_authorize_path(provider)

I'm assuming you have a User class and in your routes file you have

devise_for :users

Upvotes: 6

Justin Schier
Justin Schier

Reputation: 524

I started getting this error today (Jul 27, 2016) when I upgraded to Ruby 2.3.1 and Rails 4.2.7. The solution that worked for me was to change all instances of user_omniauth_authorize_path(:twitter) to user_twitter_omniauth_authorize_path.

Upvotes: 11

lulalala
lulalala

Reputation: 17981

One possible mistake is that omniauth configuration is set at the wrong place.

I encountered this error because I set my facebook accounts in config/initializers/omniauth.rb, as instructed by the omniauth readme.

However we need to set it through devise, i.e. config/initializers/devise.rb at the omniauth section.

Upvotes: 21

Related Questions