user1543863
user1543863

Reputation:

Cannot getting devise omniauthable working

I need to access google APIs, I'm trying to make users log in with their google account using Devise 2.1.1 and Omniauth (using the gem google_oauth2 since OAuth2 is the reccomended one from google docs).

Unfortunatly I cannot get it working, this is the routes.rb

TestApp::Application.routes.draw do
  root :to => 'Landing#index'

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

  devise_scope :user do
    get 'sign_in', :to => 'users/sessions#new', :as => :new_session
    get 'sign_out', :to => 'users/sessions#destroy', :as => :destroy_session
  end
end

Then I configured config/initializers/devise.rb to include a line with

  config.omniauth :google_oauth2, 'ID', 'SECRET', {access_type: 'offline', approval_prompt: 'force', scope: 'https://www.googleapis.com/auth/analytics.readonly'}

And actually I've a void callback class app/controllers/users/omniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
end

When I click on "Sign in with Google" I got redirected on Google, asking for authorization, then I go back to the callback, and I get redirected on the URL http://localhost:3000/sign_in.userwith the error

uninitialized constant Users::SessionsController

What's wrong here? I spent 2 day trying to figure out it by myself, I played with a lot of configurations but never get it working, it's starting frustrating me.

This is the output from rake routes

root     /                                      Landing#index
user_omniauth_authorize     /users/auth/:provider(.:format)        users/omniauth_callbacks#passthru {:provider=>/google_oauth2/}
user_omniauth_callback     /users/auth/:action/callback(.:format) users/omniauth_callbacks#(?-mix:google_oauth2)
new_session GET /sign_in(.:format)                     users/sessions#new
destroy_session GET /sign_out(.:format)                    users/sessions#destroy    

Upvotes: 4

Views: 1410

Answers (1)

Marc
Marc

Reputation: 1185

It appears you're being redirected to your sign_in route which searches for users/sessions#new per your routes.rb.

However, Users::SessionsController does not exist resulting in the error you describe.

I'm not sure in which scenarios Devise/OmniAuth redirects you to that specific route, but I imagine you can either have it go to your homepage (where people can click the Google OAuth sign in button/link again) or connect it directly to the Google OAuth page. (This may get people stuck in an infinite loop depending on how Devise/OmniAuth uses the sign_in route.)

Upvotes: 1

Related Questions