Search code examples
ruby-on-railsdeviseomniauthomniauth-google-oauth2

ActionController::RoutingError (uninitialized constant Users::OmniauthCallbacksController) Devise and google_oauth2


I have followed the tutorial from this link and keep ending up with the following log messages:

Started GET "/users/auth/google_oauth2" for 127.0.0.1 at 2019-02-22 20:59:25 +1100
I, [2019-02-22T20:59:25.512091 #11001]  INFO -- omniauth: (google_oauth2) Request phase initiated.
Started GET "/users/auth/google_oauth2/callback?state=...
I, [2019-02-22T20:59:29.060352 #11001]  INFO -- omniauth: (google_oauth2) Callback phase initiated.

ActionController::RoutingError (uninitialized constant Users::OmniauthCallbacksController):

I've searched online and all the solutions suggest checking the spelling in various files. I've included them below.

Devise _links.html.erb:

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

devise.rb:

config.omniauth :google_oauth2, client_id, client_secret, {
    scope: "contacts.readonly,userinfo.email,userinfo.profile,youtube",
    prompt: 'select_account',
    image_aspect_ratio: 'square',
    image_size: 50
}

User.rb:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:google_oauth2]

  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.token = auth.credentials.token
      user.expires = auth.credentials.expires
      user.expires_at = auth.credentials.expires_at
      user.refresh_token = auth.credentials.refresh_token
    end
  end
end

/app/controllers/users/omniauth_callbacks_controllers.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

  def google_oauth2
    @user = User.from_omniauth(request.env["omniauth.auth"])
    if @user.persisted?
      sign_in @user, :event => :authentication #this will throw if @user is not activated
      set_flash_message(:notice, :success, :kind => "Google") if is_navigational_format?
    else
      session["devise.google_data"] = request.env["omniauth.auth"]
    end
    redirect_to '/'
  end

end

routes.rb

devise_for :users, controllers: {omniauth_callbacks: "users/omniauth_callbacks"}

google console

I have set up my google console with the following Authorised redirect URLs:

http://localhost:3000/users/auth/google_oauth2/callback

Rails Routes

When I do a rails routes I have:

user_google_oauth2_omniauth_authorize GET|POST /users/auth/google_oauth2(.:format)                                                      users/omniauth_callbacks#passthru
user_google_oauth2_omniauth_callback GET|POST /users/auth/google_oauth2/callback(.:format)                                             users/omniauth_callbacks#google_oauth2

Any assistance to know why this isn't working would be greatly appreciated.


Solution

  • /app/controllers/users/omniauth_callbacks_controllers.rb

    This is not correct. You have an additional s in your controller name. This is why rails does not manage to find the class. You should rename your controller name to omniauth_callbacks_controller.rb.