user984621
user984621

Reputation: 48443

Devise and routes for omniauth

I am using Devise for authentication users, this is what is in the routes.rb file:

devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret',
                                    :confirmation => 'verification', :unlock => 'unlock', :registration => 'register',
                                    :sign_up => 'signup' }

And I added a part for omniauth (for LinkedIn):

devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret',
                                    :confirmation => 'verification', :unlock => 'unlock', :registration => 'register',
                                    :sign_up => 'signup' }, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }

In the view:

<% if user_signed_in? %>
  Signed in as <%= current_user.email %>. Not you?
  <%= link_to "Sign out", destroy_user_session_path,:method => :delete %>
  <% else %>
  <%= link_to "Sign up", new_user_registration_path %> or
  <%= link_to "Sign in", new_user_session_path %>
  <%= link_to "Sign in with Linkedin",user_omniauth_authorize_path(:linkedin) %>
<% end %>

And the error:

No route matches {:controller=>"omniauth_callbacks", :action=>"passthru", :provider=>:linkedin, :format=>nil} missing required keys: [:provider]

What am I missing here?

Thank you

Upvotes: 1

Views: 1635

Answers (2)

zehi
zehi

Reputation: 21

Verify consistent spelling of your provider in all places referenced.
The data below is an example of :facebook as the provider.

Model file with devise omniauthable call

# app/models/User.rb
  devise :omniauthable, :omniauth_providers => [:facebook]

Devise config for provider

# app/config/initializers/devise.rb
  config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'], scope: 'email', info_fields: 'email, name'

^-- Also, Register with the provider to acquire your developer APP_ID & APP_SECRET

Callbacks Controller:

# app/controllers/callbacks_controller.rb
  def facebook
    @user = User.from_omniauth(request.env["omniauth.auth"])
    ... more code here, excess omitted ...
  end

URI path within a view file

# apps/views/shared/_footer.html.erb
  user_omniauth_authorize_path(:facebook)

In this case, I only use the provider specific gem:

# app_name/Gemfile
  gem 'omniauth-facebook'

I'm not using the #omniauth gem as devise is omniauthable (we use devise.rb instead of omniauth.rb inside initialize as middleware configuration). Ref : Paragraph below Before you Start on Devise Overview wiki : https://github.com/plataformatec/devise/wiki/OmniAuth%3A-Overview

Still no love? Then I'd check the omniauth-provider gem for any specifics in use. Facebook changed their OAuth API around July - Aug 2015, so one needs this last part in the config (shown above, Devise Config, devise.rb) to get the user's email address and name :

  info_fields: 'email, name'

Upvotes: 2

Paritosh Piplewar
Paritosh Piplewar

Reputation: 8124

Do this

<% if user_signed_in? %>
  Signed in as <%= current_user.email %>. Not you?
  <%= link_to "Sign out", destroy_user_session_path,:method => :delete %>
  <% else %>
  <%= link_to "Sign up", new_user_registration_path %> or
  <%= link_to "Sign in", new_user_session_path %>
  <%= link_to "Sign in with Linkedin",user_omniauth_authorize_path(provider: :linkedin) %>
<% end %>

omniauth path takes provider option.

Upvotes: 0

Related Questions